亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 C# 中使用 NLog 記錄單個 SQL 表?

如何在 C# 中使用 NLog 記錄單個 SQL 表?

PHP
浮云間 2024-01-20 15:52:23
我正在嘗試使用 NLog 繞過參數值將單個表記錄到使用 NLog 的每列中。但不知何故我無法登錄到 SQL 表。我嘗試通過傳遞這樣的代碼來傳遞值,并在 web.config 中添加目標和規則。類中的代碼private static Logger _logger;public CustomToken(){    _logger = LogManager.GetLogger("apiUsageLogger");}_logger.Info("{clientname}", "test");_logger.Info($"clientusername", "test");_logger.Info($"route", "test");_logger.Info($"parameters", "test");_logger.Info($"isuserauthenticated", 1);在 web.config 中<target name="apiUsageLog" xsi:type="Database" connectionStringName="connStringName">        <commandtext>          INSERT INTO Table          (ClientName, ClientUserName, Route, Parameters, IsUserAuthenticated, Machine)          VALUES          (@clientname, @clientusername, @route, @parameters, @isuserauthenticated, @machine)        </commandtext>        <parameter name="@clientname" layout="${clientname}" />        <parameter name="@clientusername" layout="${clientusername}" />        <parameter name="@route" layout="${route}" />        <parameter name="@parameters" layout="${parameters}" />        <parameter name="@isuserauthenticated" layout="${isuserauthenticated}" />        <parameter name="@machine" layout="${machinename}" />      </target>    </targets>    <rules>      <<logger name="apiUsageLogger" minlevel="Info" writeTo="apiUsageLog" />    </rules>不知何故,數據沒有填充到表中。有沒有辦法可以將數據填充到適當的列中?NLog 是正確的做法嗎?
查看完整描述

1 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

不確定是否清楚,但是對于數據庫目標,一條日志消息將是數據庫中的一條記錄。同樣在這種情況下,數據庫目標選項也值得檢查。

您的配置不起作用,因為${clientusername}NLog 中不存在。

我將展示3個例子,希望能讓事情清楚

示例 1:簡單記錄到數據庫目標

記錄器調用:

logger.Info("my?info?message");

配置:

<target name="apiUsageLog" xsi:type="Database" connectionStringName="connStringName">

? ? <commandtext>

? ? ? ? INSERT INTO Table

? ? ? ? (message, machinename)

? ? ? ? VALUES

? ? ? ? (@message, @machinenameParam)

? ? </commandtext>

? ? <parameter name="@messageParam" layout="${message}" /> <!-- this will be "my info message"-->

? ? <parameter name="@machinenameParam" layout="${machinename}" /> <!-- defined in NLog, see https://nlog-project.org/config/?tab=layout-renderers-->

? ? </target>

</targets>

my info message這將在數據庫中創建一條帶有計算機名稱的日志記錄。

示例 2:使用自定義屬性:

我將在這里使用結構化日志記錄。

記錄器調用:

logger.Info("my?info?message?with?{Property1}",?"value1");

配置:

<target name="apiUsageLog" xsi:type="Database" connectionStringName="connStringName">

? ? <commandtext>

? ? ? ? INSERT INTO Table

? ? ? ? (message, machinename, property1)

? ? ? ? VALUES

? ? ? ? (@message, @machinenameParam, @propertyParam1)

? ? </commandtext>

? ? <parameter name="@messageParam" layout="${message}" /> <!-- this will be "my info message"-->

? ? <parameter name="@machinenameParam" layout="${machinename}" /> <!-- defined in NLog, see https://nlog-project.org/config/?tab=layout-renderers-->

? ? <parameter name="@propertyParam1" layout="${event-properties:Property1}" /> <!-- this will be "value1" -->

? ? </target>

</targets>

這將在數據庫中創建一條日志記錄my info message with "Value1",其中包含計算機名稱和自定義屬性“value1”。


示例3:自定義屬性,不全部在消息中

這結合了結構化日志記錄和WithProperty. 為此,您至少需要 NLog 4.6.3。


記錄器調用:


logger.WithProperty("Property2", "value2")

? ? ? .Info("my info message {Property1}", "value1");


配置:


<target name="apiUsageLog" xsi:type="Database" connectionStringName="connStringName">

? ? <commandtext>

? ? ? ? INSERT INTO Table

? ? ? ? (message, machinename, property1, property2)

? ? ? ? VALUES

? ? ? ? (@message, @machinenameParam, @propertyParam2)

? ? </commandtext>

? ? <parameter name="@messageParam" layout="${message}" /> <!-- this will be: my info message with "value1"-->

? ? <parameter name="@machinenameParam" layout="${machinename}" /> <!-- defined in NLog, see https://nlog-project.org/config/?tab=layout-renderers-->

? ? <parameter name="@propertyParam1" layout="${event-properties:Property1}" /> <!-- this will be "value1" -->

? ? <parameter name="@propertyParam2" layout="${event-properties:Property2}" /> <!-- this will be "value2" -->

? ? </target>

</targets>

這將在數據庫中創建一條日志記錄my info message with "Value1",其中包含計算機名稱以及自定義屬性“value1”和“value2”


請注意,現在消息中包含“value1”,而“value2”則不在消息中。


查看完整回答
反對 回復 2024-01-20
  • 1 回答
  • 0 關注
  • 164 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號