簡體   English   中英

Nlog $ {event-context:item = xxxx}未寫入日志數據庫

[英]Nlog ${event-context:item=xxxx} not writing in logging database

我有一個Nlog配置,基本上是這樣的:

<target type="Database" name="database" connectionstring="<working connection string>">
  <commandText>insert into LogEntries ([Username], [Process], [TransactionCode], [WorkplaceId], [CreateDate], [Origin], [LogLevel], [Message], [Exception], [StackTrace]) values (@username, @process, @transactionCode, @workplaceId, @createDate, @origin, @logLevel, @message, @exception, @stackTrace);</commandText>

  <parameter name="@username" layout="${event-context:item=username}"/>
  <parameter name="@process" layout="${event-context:item=process}"/>
  <parameter name="@transactionCode" layout="${event-context:item=transactionCode}"/>
  <parameter name="@workplaceId" layout="${event-context:item=workplaceId}"/>
  <parameter name="@createDate" layout="${date}"/>
  <parameter name="@origin" layout="${callsite}"/>
  <parameter name="@logLevel" layout="${level}"/>
  <parameter name="@message" layout="${message}"/>
  <parameter name="@exception" layout="${exception:format=message}"/>
  <parameter name="@stackTrace" layout="${onexception: ${stacktrace}}"/>
</target>

日志記錄基本上可以正常工作,並且標准Nlog項已成功寫入數據庫,但是事件上下文項在數據庫中的寫入方式類似於空字符串。

我已經檢查了eventInfo對象上的Properties項目,它已正確填充。

未知的Nlog狀態

有人對我想念的東西有想法嗎? 我不知道這可能是什么。

事實證明,這是NLog中的錯誤(或功能)。 當異步寫入NLog中的“目標”時,寫入的原始LogEventInfo將添加到新LogEventInfoParameters集合中,因此{event-context}從中獲取其值的Properties集合為空(因為它是新的賓語)。 結果將空字符串寫入數據庫。

因此,為了解決此問題,我將此功能添加到了<< NLog 2.1 Source >> \\ Targets \\ Target.cs:

    private void MergeEventProperties(LogEventInfo logEvent)
    {
        foreach (var item in logEvent.Parameters)
        {
            if (item.GetType() == typeof(LogEventInfo))
            {

                foreach (var propertyItem in ((LogEventInfo)item).Properties)
                {
                    logEvent.Properties.Remove(propertyItem.Key);
                    logEvent.Properties.Add(propertyItem);
                }
            }
        }
    }

然后在函數protected virtual void Write(AsyncLogEventInfo logEvent)函數protected virtual void Write(AsyncLogEventInfo logEvent)我更改了此代碼

        try
        {
            this.Write(logEvent.LogEvent);
            logEvent.Continuation(null);
        }

到這個:

        try
        {
            MergeEventProperties(logEvent.LogEvent);

            this.Write(logEvent.LogEvent);
            logEvent.Continuation(null);
        }

之后,我的值被寫入數據庫。

希望對其他遇到此問題的人有所幫助。

您必須手動填寫LogEvent.Properties,否則所有事件上下文項都將呈現“”。 這些不是自動生成的

https://github.com/nlog/nlog/wiki/EventProperties-Layout-Renderer

NLog文檔中的事件上下文已更改為事件屬性

感謝您的修復。 自從您的原始位置以來,此問題已稍有發展。

追了所有的補丁GitHub的(后334331371與此相關的問題),並檢查他們沒有被逆轉,我發現${event-context:item=x}似乎不正常工作${event-properties:item=x}可以了。 公平地說,他們確實提到上下文已過時。 因此,建議您更改NLog.config以使用event-properties

哦,我正在使用NLog v4.1.0和NLog.Extended v4.0.0.1,以防將來出現這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM