Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net.Ext.Json custom properties not appearing in json

Tags:

json

log4net

We are setting log4net properties but they aren't appearing in the log file outputted by log4net.ext.json.

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

However, our log doesn't include these using the default configs specified in the wiki. Am I doing it wrong?

Our log4net config looks like this:

<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>

I get the standard fields (and it is gloriously consumed by Splunk) but not the custom ones. Any suggestions?

like image 826
Code Silverback Avatar asked Jun 20 '14 16:06

Code Silverback


1 Answers

It was fixed for a while but not released. There was a glitch with reading the properties from the event. It is fixed in revision 26. Please try the new release 20140623 - https://sourceforge.net/projects/log4net-json/files/20140623/

Oh and add a member for the property - either "property" or "OurCompany.ApplicationName"

Alternatively, for static content like this, you might want to try add a pattern layout member (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");
    }
}

And finally, try member "appname".

Thanks, Rob

like image 59
Robert Cutajar Avatar answered Oct 22 '22 07:10

Robert Cutajar