log4net.Ext.Json自定义属性不出现在json中

时间:2014-06-20 16:11:04

标签: json log4net

我们正在设置log4net属性,但它们不会出现在log4net.ext.json输出的日志文件中。

log4net.GlobalContext.Properties["OurCompany.ApplicationName"] ="fubar";

但是,我们的日志并未使用wiki中指定的默认配置包含这些内容。我做错了吗?

我们的log4net配置如下所示:

<log4net debug="false">
  <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString">
      <conversionPattern value="%env{MY_LOG_DIR}\%property{OurCompany.ApplicationName}\%property{OurCompany.ApplicationName}_%date{yyyy-MM-dd}.log" />
    </file>
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json">
      <decorator type='log4net.Layout.Decorators.StandardTypesDecorator, log4net.Ext.Json' />
      <default />
      <!--explicit default members-->
      <remove value='message' />
      <!--remove the default preformatted message member-->
      <member value='message:messageobject' />
      <!--add raw message-->
    </layout>
  </appender>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="FileAppender" />
  </root>
</log4net>

我得到标准字段(并且它被Splunk光荣地消耗)但不是自定义字段。有什么建议吗?

1 个答案:

答案 0 :(得分:4)

它被修复了一段时间但没有被释放。从事件中读取属性有一个小问题。它在修订版26中修复。请尝试新版本20140623 - https://sourceforge.net/projects/log4net-json/files/20140623/

哦,并为该物业添加一名成员 - &#34; property&#34;或&#34; OurCompany.ApplicationName&#34;

或者,对于像这样的静态内容,您可能想尝试添加模式布局成员(A):

public class Members : RepoTest
{
    protected override string GetConfig()
    {
        return @"<log4net>
                    <root>
                      <level value='DEBUG'/>
                      <appender-ref ref='TestAppender'/>
                    </root>

                    <appender name='TestAppender' type='log4net.Ext.Json.Tests.General.TestAppender, log4net.Ext.Json.Tests'>
                      <layout type='log4net.Layout.SerializedLayout, log4net.Ext.Json'>
                        <member value='OurCompany.ApplicationName' /> <!-- ref to property -->
                        <member value='A|L-%p-%c' /> <!-- (|) arbitrary pattern layout format -->
                        <member value='B%date:yyyy' /> <!-- (%:) one pattern layout conversion pattern with optional option -->
                        <member value='Host=ProcessId\;HostName' /> <!-- (=) nested structure, escape ; -->
                        <member value='App:appname' /> <!-- named member -->
                      </layout>
                    </appender>
                  </log4net>";
    }

    protected override void TestLog(log4net.ILog log)
    {
        log4net.GlobalContext.Properties["OurCompany.ApplicationName"] = "fubar";

        log.Info(4);

        var events = GetEventStrings(log.Logger);

        Assert.AreEqual(1, events.Length, "events Count");

        var le = events.Single();

        Assert.IsNotNull(le, "loggingevent");

        var procid = Process.GetCurrentProcess().Id;

        StringAssert.StartsWith(@"{""OurCompany.ApplicationName"":""fubar""", le, "log line");
        StringAssert.Contains(@",""Host"":{", le, "log line");
        StringAssert.Contains(@"""ProcessId"":" + procid, le, "log line");
        StringAssert.Contains(@"""HostName"":""" + Environment.MachineName + @"""", le, "log line");
        StringAssert.Contains(@"""A"":""L-INFO-log4net.Ext.Json.Tests.Layout.Arrangements.Members""", le, "log line");
        StringAssert.Contains(@"""B"":""" + DateTime.Now.Year + @"""", le, "log line");
        StringAssert.Contains(@"""App"":""", le, "log line");
    }
}

最后,试试会员&#34; appname&#34;。

谢谢,Rob