3

I'm trying to patch an AdoNetAppender using the log4net.Appender.AdoNetAppender, log4net appender by doing the following:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <log4net>
            <appender name="AdoNetAppender" patch:after="*[@name='PublishingLogFileAppender']" type="log4net.Appender.AdoNetAppender, log4net">
                <bufferSize value="1" />
                <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <connectionStringName value="Log4netConnectionString" />

By enabling the log4net internal debbuging, I noticed the following error:

log4net: DOMConfigurator: Loading Appender [AdoNetAppender] type: [log4net.Appender.AdoNetAppender, log4net]

log4net:ERROR DOMConfigurator: Could not create Appender [AdoNetAppender] of type [log4net.Appender.AdoNetAppender, log4net]. Reported error follows. System.InvalidCastException: Unable to cast object of type 'log4net.Appender.AdoNetAppender' to type 'log4net.Appender.IAppender'. at log4net.Repository.Hierarchy.DOMHierarchyConfigurator.ParseAppender(XmlElement appenderElement)

log4net:ERROR DOMConfigurator: Appender named [AdoNetAppender] not found.

I wonder if Sitecore is trying to cast the log4net.Appender.AdoNetAppender, log4net to type log4net.Appender.IAppender, Sitecore.Logging.

First question would be: Is it possible to use log4net.Appender.AdoNetAppender, log4net appender?

I tried to use the log4net.Appender.AdoNetAppender, Sitecore.Logging before, but it is very limited. e.g. there is not RawPropertyLayout and the PatternLayout doesn't work the same way as in the log4net assembly.

Update 1

If I change the type to log4net.Appender.AdoNetAppender (without the , log4net) as below, I get errors such as [log4net.Layout.PatternLayout] is not assignable to type [log4net.Layout.IRawLayout].

<appender name="AdoNetAppender" patch:after="*[@name='PublishingLogFileAppender']" type="log4net.Appender.AdoNetAppender">
    [...]
    <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.PatternLayout, log4net">
            <conversionPattern value="%utcdate{yyyy&apos;-&apos;MM&apos;-&apos;dd HH&apos;:&apos;mm&apos;:&apos;ss&apos;.&apos;fff}" />
        </layout>
    </parameter>

I believe it is due to the fact that I'm using log4net.Layout.PatternLayout, log4net layouts.

log4net:ERROR DOMConfigurator: Object type [log4net.Layout.PatternLayout] is not assignable to type [log4net.Layout.IRawLayout]. There are no acceptable type convertions. log4net:ERROR DOMConfigurator: Failed to create object to set param: layout

Also, I get the following error:

log4net:ERROR DOMConfigurator: Cannot find Property [connectionStringName] to set object on [log4net.Appender.ADONetAppender]

I believe Sitecore is assuming log4net.Appender.AdoNetAppender, Sitecore.Logging if I omit the log4net from the type.

  • Can you try with type="log4net.Appender.ADONetAppender"? Just checking a site where I have this working and that was the first difference. – Gatogordo Apr 6 '18 at 8:58
  • @Gatogordo I tried it also, please see my Update 1 above. – João Neto Apr 6 '18 at 9:10
0

After a while, I figure it out. Here's what I found out:

  • Although Sitecore.Logging assembly uses the same log4net namespace, it doesn't depend on log4net at all. You can check it in the Sitecore Assembly list link in the Release information section of the download page and also in the .zip installation file. i.e. you won't find a log4net assembly.
  • Sitecore.Logging seems like a copy-and-paste from the original log4net but with fewer features. e.g. you don't have the RawPropertyLayout and the PatternLayout doesn't behave as the one in log4net (version 2.0.8).
  • When you omit the "log4net" assembly name in the appender type, Sitecore assumes you are using an appender available at Sitecore.Logging. e.g. log4net.Appender.AdoNetAppender.
  • If you use the "log4net" assembly name in the appender type, Sitecore tries to parse it to log4net.Appender.IAppender, Sitecore.Logging, which obviously causes a parse error.

How to work this around?

If you want to use the same features from log4net you have two options:

1) Create a custom appender/layout extending from log4net.Appender.IAppender, Sitecore.Logging or log4net.Appender.IRawLayout, Sitecore.Logging and copy the code from the log4net.

2) Configure log4net separately by either adding a log4net.config file or a different one but referencing its location in the application settings. e.g.

<appSettings>
  <add key="log4net.Config" value="App_Config/Company.Log4net.config"/>
  <add key="log4net.Config.Watch" value="True"/>
</appSettings>

See more about log4net configuration here.

Don't replace the log4net section in the web.config. Otherwise, the Sitecore logging will stop working.

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.