NLOG $ {事件背景:项目= XXXX}没有记录数据库写 [英] Nlog ${event-context:item=xxxx} not writing in logging database

查看:359
本文介绍了NLOG $ {事件背景:项目= XXXX}没有记录数据库写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NLOG配置,基本上是这样的:

I have a Nlog configuration that is basically this:

<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项目成功写入到数据库中,但该事件上下文的项目是这样写数据库中的空字符串。

The logging basically works, and the standard Nlog items are successfully written to the database, but the event-context items are written like empty string in the database.

我已经检查了eventInfo对象的属性项,并正确填写。

I already checked the Properties item on the eventInfo object and it is filled correctly.

上的任何人,我缺少的是什么想法?我出的可能是什么想法。

Anyone an idea on what I am missing? I am out of ideas of what it could be.

推荐答案

事实证明,这是一个错误(或要素)。当写在NLOG一个目标异步原 LogEventInfo 被写入被加入到了参数集合新的 LogEventInfo ,因此,属性集合,其中的 {事件上下文} 获取其值。导致写入到数据库中的空字符串。

As it turned out, it was a bug (or a feature) in NLog. When writing to a 'target' in NLog asynchronously the original LogEventInfo that is written is added to the Parameters collection of a new LogEventInfo, thus the Properties collection where the {event-context} gets its values from is empty (because it is a new object). Resulting in the empty strings written to the database.

因此​​,为了解决这个我添加了这个功能到&lt;&LT; NLOG 2.1源代码>> \\目标\\ Target.cs:

So, in order to solve this I added this function to << 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);
                }
            }
        }
    }

然后在功能受保护的虚拟无效写(AsyncLogEventInfo LOGEVENT)我改变了这个code

Then in the function protected virtual void Write(AsyncLogEventInfo logEvent) I changed this code

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

这个:

        try
        {
            MergeEventProperties(logEvent.LogEvent);

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

在此之后,我的值被写入到数据库中。

After that, my values were written to the database.

我希望它能帮助谁是有这个问题其他人。

I hope it helps other people who are having this issue.

这篇关于NLOG $ {事件背景:项目= XXXX}没有记录数据库写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆