0

I am using the Log4Net to insert errors into MSSQL. However, the errors are not being logged into the database.

Log4net Patch File - z.Logging.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <log4net>
        <appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender">
            <bufferSize value="1" />
            <lossy value="true" />
            <evaluator type="log4net.Core.LevelEvaluator">
               <threshold value="INFO" />
            </evaluator>
            <appender-ref ref="ADONetAppender_SqlServer" />
         </appender>
         <appender name="ADONetAppender_SqlServer" type="log4net.Appender.ADONetAppender, Sitecore.Logging">
            <bufferSize value="1" />
            <param name="ConnectionType" value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <param name="ConnectionString" value="user id=[userId];password=[password];Data Source=ServerName\CD_CUSTOM;Database=Sitecore.Logging" />
            <param name="CommandText" value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
            <param name="Parameter">
               <param name="ParameterName" value="@log_date" />
               <param name="DbType" value="DateTime" />
               <param name="Layout" type="log4net.Layout.PatternLayout">
                  <param name="ConversionPattern" value="%d{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
               </param>
            </param>
            <param name="Parameter">
               <param name="ParameterName" value="@thread" />
               <param name="DbType" value="String" />
               <param name="Size" value="255" />
               <param name="Layout" type="log4net.Layout.PatternLayout">
                  <param name="ConversionPattern" value="%t" />
               </param>
            </param>
            <param name="Parameter">
               <param name="ParameterName" value="@log_level" />
               <param name="DbType" value="String" />
               <param name="Size" value="50" />
               <param name="Layout" type="log4net.Layout.PatternLayout">
                  <param name="ConversionPattern" value="%p" />
               </param>
            </param>
            <param name="Parameter">
               <param name="ParameterName" value="@logger" />
               <param name="DbType" value="String" />
               <param name="Size" value="255" />
               <param name="Layout" type="log4net.Layout.PatternLayout">
                  <param name="ConversionPattern" value="%c" />
               </param>
            </param>
            <param name="Parameter">
               <param name="ParameterName" value="@message" />
               <param name="DbType" value="String" />
               <param name="Size" value="4000" />
               <param name="Layout" type="log4net.Layout.PatternLayout">
                  <param name="ConversionPattern" value="%m" />
               </param>
            </param>
         </appender>
         <root>
            <priority value="INFO" />
            <appender-ref ref="LogFileAppender">
                <patch:attribute name="ref">ADONetAppender_SqlServer</patch:attribute>
            </appender-ref>
         </root>
         <logger name="Sitecore.Diagnostics.WebDAV" additivity="false">
            <level value="INFO" />
            <appender-ref ref="WebDAVLogFileAppender">
                <patch:attribute name="ref">ADONetAppender_SqlServer</patch:attribute>
            </appender-ref>            
         </logger>
         <logger name="Sitecore.Diagnostics.Publishing" additivity="false">
            <level value="INFO" />
            <appender-ref ref="PublishingLogFileAppender">
                <patch:attribute name="ref">ADONetAppender_SqlServer</patch:attribute>
            </appender-ref>
         </logger>
         <logger name="Sitecore.Diagnostics.Crawling" additivity="false">
            <level value="INFO" />
            <encoding value="utf-8" />
            <appender-ref ref="CrawlingLogFileAppender">
                <patch:attribute name="ref">ADONetAppender_SqlServer</patch:attribute>
            </appender-ref>
         </logger>
         <logger name="Sitecore.Diagnostics.Search" additivity="false">
            <level value="INFO" />
            <encoding value="utf-8" />
            <appender-ref ref="SearchLogFileAppender">
                <patch:attribute name="ref">ADONetAppender_SqlServer</patch:attribute>
            </appender-ref>
         </logger>
      </log4net>
    </sitecore>
</configuration>

Note

The same configuration has been applied on my local instance and everything works, i.e, the errors are logged into the table Log. However, on the Staging Environment, no errors are being logged.

I have also tried to modified the Sitecore.config directly and use the following configuration:

<log4net>
    <appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender">
        <bufferSize value="1" />
        <lossy value="true" />
        <evaluator type="log4net.Core.LevelEvaluator">
           <threshold value="INFO" />
        </evaluator>
        <appender-ref ref="ADONetAppender_SqlServer" />
     </appender>
     <appender name="ADONetAppender_SqlServer" type="log4net.Appender.ADONetAppender, Sitecore.Logging">
        <bufferSize value="1" />
        <param name="ConnectionType" value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <param name="ConnectionString" value="user id=[userId];password=[password];Data Source=ServerName\CD_CUSTOM;Database=Sitecore.Logging" />
        <param name="CommandText" value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
        <param name="Parameter">
           <param name="ParameterName" value="@log_date" />
           <param name="DbType" value="DateTime" />
           <param name="Layout" type="log4net.Layout.PatternLayout">
              <param name="ConversionPattern" value="%d{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
           </param>
        </param>
        <param name="Parameter">
           <param name="ParameterName" value="@thread" />
           <param name="DbType" value="String" />
           <param name="Size" value="255" />
           <param name="Layout" type="log4net.Layout.PatternLayout">
              <param name="ConversionPattern" value="%t" />
           </param>
        </param>
        <param name="Parameter">
           <param name="ParameterName" value="@log_level" />
           <param name="DbType" value="String" />
           <param name="Size" value="50" />
           <param name="Layout" type="log4net.Layout.PatternLayout">
              <param name="ConversionPattern" value="%p" />
           </param>
        </param>
        <param name="Parameter">
           <param name="ParameterName" value="@logger" />
           <param name="DbType" value="String" />
           <param name="Size" value="255" />
           <param name="Layout" type="log4net.Layout.PatternLayout">
              <param name="ConversionPattern" value="%c" />
           </param>
        </param>
        <param name="Parameter">
           <param name="ParameterName" value="@message" />
           <param name="DbType" value="String" />
           <param name="Size" value="4000" />
           <param name="Layout" type="log4net.Layout.PatternLayout">
              <param name="ConversionPattern" value="%m" />
           </param>
        </param>
     </appender>
    <appender name="LogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
      <file value="$(dataFolder)/logs/log.{date}.txt"/>
      <appendToFile value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n"/>
      </layout>
      <encoding value="utf-8"/>
    </appender>
    <appender name="WebDAVLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
      <file value="$(dataFolder)/logs/WebDAV.log.{date}.txt"/>
      <appendToFile value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n"/>
      </layout>
      <encoding value="utf-8"/>
    </appender>
    <appender name="PublishingLogFileAppender" type="log4net.Appender.SitecoreLogFileAppender, Sitecore.Logging">
      <file value="$(dataFolder)/logs/Publishing.log.{date}.txt"/>
      <appendToFile value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n"/>
      </layout>
      <encoding value="utf-8"/>
    </appender>
    <root>
      <priority value="INFO"/>
      <appender-ref ref="BufferingForwardingAppender"/>
    </root>
    <logger name="Sitecore.Diagnostics.WebDAV" additivity="false">
      <level value="INFO"/>
      <appender-ref ref="BufferingForwardingAppender"/>
    </logger>
    <logger name="Sitecore.Diagnostics.Publishing" additivity="false">
      <level value="INFO"/>
      <appender-ref ref="BufferingForwardingAppender"/>
    </logger>
    <logger name="Sitecore.Diagnostics.Crawling" additivity="false">
      <level value="INFO" />
      <encoding value="utf-8" />
      <appender-ref ref="BufferingForwardingAppender" />
    </logger>
    <logger name="Sitecore.Diagnostics.Search" additivity="false">
      <level value="INFO" />
      <encoding value="utf-8" />
      <appender-ref ref="BufferingForwardingAppender" />
    </logger>
  </log4net>

But still it worked on my local instance and not on the Staging Server.

Moreover, I have setup a profiler to see if there are any request coming through but in vain. Nothing is being obtained. I have also set the db_owner permission on the User.

I am making use of Sitecore 8.2 Update 1

Any help?

1

Assuming that you already checked that the configs of local and staging are in sync: did you try with log4net internal debugging? When log4net is working on one location and not on the other this has helped me a few times.

Try enabling this and (re-)start your application. If the connection cannot be established (as seems the case) -e.g. if anything is wrong with your connection string- it will log this. You can read here how to set internal debugging:

Internal debugging can be enabled by setting a value in the application's configuration file. The log4net.Internal.Debug application setting must be set to the value true. For example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
</configuration>

This setting is read immediately on startup an will cause all internal debugging messages to be emitted.

As log4net internal debug messages are written to the System.Diagnostics.Trace system it is possible to redirect those messages to a local file. You can define a trace listener by adding the following to your application's .config file:

<configuration>
    ...
    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add 
                    name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData="C:\...\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>
    ...
</configuration>

Make sure that the process running your application has permission to write to this file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.