skip to Main Content

I am new to logging in .Net and was trying to learn log4net as a logging tool. I am running into a problem which is
I have created a log4net config file and trying to add values of logs into SSMS but values are not populating as expected. Here is my log4net config file

<?xml version="1.0" encoding="utf-8" ?>

<log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %5level %logger.%method [%line] - %message%newline" />
        </layout>
    </appender>

    <appender name="DatabaseAppender" type="ToolsCommon.logging.AdoNetAppender">
        <filter type="log4net.Filter.LoggerMatchFilter">
            <loggerToMatch value="Startup" />
            <acceptOnMatch value="false" />
        </filter>
        <bufferSize value="1" />
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />0
        <connectionString value="dynamicly created" />
        <commandText value="INSERT INTO Log ([EntryID],[Message],[TimeStamp],[SeverityId]) VALUES (@entryid,  @message, @log_date, @severityId)" />
        <parameter>
            <parameterName value="@entryid" />
            <dbType value="String" />
            <layout type="log4net.Layout.PatternLayout">
                
                <conversionPattern value="%entryid" />
            </layout>
        </parameter>
        
        <parameter>
            <parameterName value="@log_date" />
            <dbType value="DateTime" />
            <layout type="log4net.Layout.RawTimeStampLayout" />
        </parameter>
        <parameter>
            <parameterName value="@message" />
            <dbType value="String" />
            <size value="4000" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%message" />
            </layout>
        </parameter>
        
        <parameter>
            <parameterName value="@severityId" />
            <dbType value="Int64" />
            <layout type="log4net.Layout.PatternLayout">
                <converter>
                    <name value="severityId" />
                    <type value="ToolsCommon.logging.QCSeverityConverter" />
                </converter>
                <conversionPattern value="%severityId" />
            </layout>
        </parameter>
    </appender>

    <appender name="FileAppender" type="ToolsCommon.logging.CustomFileAppender">
        <file value="%property{logFilePath}/ToolLog.log" />
        <appendToFile value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %5level %logger.%method [%line] - %message%newline" />
        </layout>
    </appender>

    <root>
        <level value="ALL"/>
        <appender-ref ref="ConsoleAppender" />
        <appender-ref ref="DatabaseAppender" />
        <appender-ref ref="FileAppender" />
    </root>
</log4net>

Issue with above code is EntryID is not getting populated as expected rather it gets populated with some other value which I have no idea where that value is coming from.

Is there any way I can debug this and find out how EntryID value is populated?

I tried debugging it but since this is a config file I cannot debug it using Visual Studios debugger. I think this is getting invoked as and when logger is called but I am not sure where EntryId value is coming from? I am expecting any clue so I can figure out how EntryId is getting populated.

2

Answers


  1. You can turn on internal debugging with:

    <configuration>
        <appSettings>
            <add key="log4net.Internal.Debug" value="true"/>
        </appSettings>
    </configuration>
    

    see here for more info.

    Login or Signup to reply.
  2. Please edit your configuration file according to this format:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="log4net"  type="log4net.Config.Log4NetConfigurationSectionHandler,
                log4net-net-1.0" />
      </configSections>
    
      <log4net>
        ..
      </log4net>
    </configuration>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search