2

A solution already exists that uses log4net with Common.Logging, we have decided we want to swap out log4net and swap in Serilog.

In App.config

--<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1215">
++<factoryAdapter type="Common.Logging.Serilog.SerilogFactoryAdapter, Common.Logging.Serilog">

Seems easy enough, but then as I continued looking through our existing App.config file I came across less obvious areas of transition.

--<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
++<section name="serilog" type="Serilog.Configuration.LoggerSettingsConfiguration, serilog"/><!--Not sure if equivalent-->

This change does not seem as obviously right and is really just a shot in the dark. In fact, I'm pretty sure it is wrong.

Then there is the actual config of the rolling file appender:

    <log4net>
    <appender name="LM" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="%property{LogFileName}\\Program\Logs\\specificProgram.log" />
      <param name="RollingStyle" value="Size" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="5" />
      <param name="MaximumFileSize" value="10MB" />
      <param name="StaticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d{MM-dd-yy HH:mm:ss.fff} [%thread] %-5p - %c{1} - %m%n" />
      </layout>
    </appender>
    <appender name="QuickLog" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="%property{LogFileName}\\Program\Logs\\QuickLog.log" />
      <param name="RollingStyle" value="Size" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="5" />
      <param name="MaximumFileSize" value="10MB" />
      <param name="StaticLogFileName" value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d{MM-dd-yy HH:mm:ss.fff} [%thread] %-5p - %c{1} - %m%n" />
      </layout>
    </appender>
    <logger name="LM">
      <level value="TRACE"/>
      <appender-ref ref="LM"/>
    </logger>
    <logger name="QuickLog">
      <level value="TRACE"/>
      <appender-ref ref="QuickLog"/>
    </logger>
  </log4net>

I couldn't find any blogs or documentation on swapping from log4net to Serilog that use mainly App.config for settings...

In the code behind, we have a pretty simple initialization

 if (null == s_logger || null == quick_logger)
        {                
            log4net.GlobalContext.Properties["LogFileName"] = MachineConfiguration.DataPath;
            s_logger = CommonLogging.LogManager.GetLogger("LM");
            quick_logger = CommonLogging.LogManager.GetLogger("QuickLog");
        }

Serilog also seems to lack a GlobalContext, or at least I haven't found the equivalent yet looking through the library.

Is there any mapping of App.config changes from log4net to Serilog? or somewhere to look up what the equivalent types or params are between the two libraries?

I know I can't be the first person to do this swap, but I am having trouble finding documentation that matches what we currently have in place.

M Y
  • 1,831
  • 4
  • 24
  • 52
  • 2
    In case it helps - Serilog's `` configuration syntax is discussed in: github.com/serilog/serilog-settings-appsettings – Nicholas Blumhardt Jun 17 '20 at 06:01
  • FWIW, I think you'd get the best result here by setting up Serilog how you like it from scratch, using its C# API, and then wiring it to Common.Logging/XML config. Mapping concepts 1:1 could lead to a lot of awkwardness, given the substantial differences between the two libraries. HTH! – Nicholas Blumhardt Jun 17 '20 at 06:04
  • @NicholasBlumhardt thanks so much. That link is definitely helpful. So glad that there is a way to pass variable to the appconfig for changing the path. That was the biggest concern. So relieved. I'll have to reread the doc again tomorrow morning, but tonight I can sleep in peace. – M Y Jun 17 '20 at 06:08

0 Answers0