log4mongo / log4mongo-net

log4net appender to MongoDB database.
http://log4mongo.org
BSD 3-Clause "New" or "Revised" License
135 stars 65 forks source link

Multiple appenders share the same properties? #22

Open izmailoff opened 9 years ago

izmailoff commented 9 years ago

Hi,

I'm not sure if my configuration is wrong or if that's a bug but when I configure 2 appenders to write to two different collections the second appender configuration seems to overwrite the first one. Here is my config:

    private const string eventLogTemplateConf = @"
    <log4net>

      <appender name=""EventLogAppender"" type=""Log4Mongo.MongoDBAppender, Log4Mongo"">

        <connectionString value=""mongodb://username:password@localhost/log"" />

        <collectionName value=""events"" />

        <field>
          <name value=""timestamp"" />
          <layout type=""log4net.Layout.RawTimeStampLayout"" />
        </field>

        <field>
          <name value=""hostname"" />
          <layout type=""log4net.Layout.PatternLayout"">
            <conversionPattern value=""%property{log4net:HostName}"" />
          </layout>
        </field>

        <field>
          <name value=""invoker"" />
          <layout type=""log4net.Layout.RawPropertyLayout"">
            <key value=""invoker"" />
          </layout>
        </field>

        <field>
          <name value=""level"" />
          <layout type=""log4net.Layout.PatternLayout"" value=""%level"" />
        </field>

        <field>
          <name value=""thread"" />
          <layout type=""log4net.Layout.PatternLayout"" value=""%thread"" />
        </field>

        <field>
          <name value=""message"" />
          <layout type=""log4net.Layout.PatternLayout"" value=""%message"" />
        </field>

        <field>
          <name value=""ctx"" />
          <layout type=""log4net.Layout.RawPropertyLayout"">
            <key value=""ctx"" />
          </layout>
        </field>

      </appender>

      <root>
        <level value=""ALL"" />
        <appender-ref ref=""EventLogAppender"" />
      </root>

    </log4net>";

            private const string profileLogTemplateConf = @"
    <log4net>

      <appender name=""ProfilerAppender"" type=""Log4Mongo.MongoDBAppender, Log4Mongo"">

        <connectionString value=""mongodb://username:password@localhost/log"" />

        <collectionName value=""slowOps"" />

        <field>
          <name value=""timestamp"" />
          <layout type=""log4net.Layout.RawTimeStampLayout"" />
        </field>

        <field>
          <name value=""hostname"" />
          <layout type=""log4net.Layout.PatternLayout"">
            <conversionPattern value=""%property{log4net:HostName}"" />
          </layout>
        </field>

        <field>
          <name value=""invoker"" />
          <layout type=""log4net.Layout.RawPropertyLayout"">
            <key value=""invoker"" />
          </layout>
        </field>

        <field>
          <name value=""level"" />
          <layout type=""log4net.Layout.PatternLayout"" value=""%level"" />
        </field>

        <field>
          <name value=""thread"" />
          <layout type=""log4net.Layout.PatternLayout"" value=""%thread"" />
        </field>

        <field>
          <name value=""message"" />
          <layout type=""log4net.Layout.PatternLayout"" value=""%message"" />
        </field>

        <field>
          <name value=""ctx"" />
          <layout type=""log4net.Layout.RawPropertyLayout"">
            <key value=""ctx"" />
          </layout>
        </field>

      </appender>

      <root>
        <level value=""ALL"" />
        <appender-ref ref=""ProfilerAppender"" />
      </root>

    </log4net>";

I get a logger this way:

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(eventLogTemplateConf); // load config from string above
        XmlConfigurator.Configure(doc.DocumentElement);
        // loggerName here is either "ProfilerAppender" or "EventLogAppender" from above
        var myLogger = LogManager.GetLogger(loggerName); 

So I get two loggers. It happens so that I get ProfilerAppender first. When I use those loggers to log, messages get properly saved to db, except that they all get saved to the same collection (events). Instead the desired outcome is that ProfilerAppender saves to slowOps and EventLogAppender to events collection.

I enabled debugging log4net.Util.LogLog.InternalDebugging = true; to see what's happening and this is what I get:

log4net: log4net assembly [log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a]. Loaded from [C:\Users\Aleksey\AppData\Local\Temp\Temporary ASP.NET Files\root\84fdf85b\acee8338\assembly\dl3\982ca964\1a24f491_604ad001\log4net.dll]. (.NET Runtime [4.0.30319.34209] on Microsoft Windows NT 6.2.9200.0)
log4net: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository for assembly [CXA.Common, Version=1.0.0.48, Culture=neutral, PublicKeyToken=null]
log4net: Assembly [CXA.Common, Version=1.0.0.48, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Users\Aleksey\AppData\Local\Temp\Temporary ASP.NET Files\root\84fdf85b\acee8338\assembly\dl3\94b13a2d\91b598b9_145ad001\CXA.Common.dll]
log4net: Assembly [CXA.Common, Version=1.0.0.48, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: Assembly [CXA.Common, Version=1.0.0.48, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
log4net: Configuring Repository [log4net-default-repository]
log4net: Configuration update mode [Merge].
log4net: Logger [root] Level string is [ALL].
log4net: Logger [root] level set to [name="ALL",value=-2147483648].

log4net: Loading Appender [ProfilerAppender] type: [Log4Mongo.MongoDBAppender, Log4Mongo]
log4net: Setting Property [ConnectionString] to String value [mongodb://username:password@localhost:27017/log]
log4net: Setting Property [CollectionName] to String value [slowOps]
log4net: Setting Property [Name] to String value [timestamp]
log4net: Setting Property [Layout] to object [log4net.Layout.RawTimeStampLayout]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [hostname]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [ConversionPattern] to String value [%property{log4net:HostName}]
log4net: Converter [property] Option [log4net:HostName] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [Layout] to object [log4net.Layout.Layout2RawLayoutAdapter]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [invoker]
log4net: Setting Property [Key] to String value [invoker]
log4net: Setting Property [Layout] to object [log4net.Layout.RawPropertyLayout]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [level]
log4net: Parameter [layout] specified subtype [log4net.Layout.PatternLayout]
log4net: Converter [level] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Performing additional conversion of value from [PatternLayout] to [IRawLayout]
log4net: Setting Property [Layout] to Layout2RawLayoutAdapter value [log4net.Layout.Layout2RawLayoutAdapter]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [thread]
log4net: Parameter [layout] specified subtype [log4net.Layout.PatternLayout]
log4net: Converter [thread] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Performing additional conversion of value from [PatternLayout] to [IRawLayout]
log4net: Setting Property [Layout] to Layout2RawLayoutAdapter value [log4net.Layout.Layout2RawLayoutAdapter]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [message]
log4net: Parameter [layout] specified subtype [log4net.Layout.PatternLayout]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Performing additional conversion of value from [PatternLayout] to [IRawLayout]
log4net: Setting Property [Layout] to Layout2RawLayoutAdapter value [log4net.Layout.Layout2RawLayoutAdapter]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [ctx]
log4net: Setting Property [Key] to String value [ctx]
log4net: Setting Property [Layout] to object [log4net.Layout.RawPropertyLayout]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Created Appender [ProfilerAppender]

log4net: Adding appender named [ProfilerAppender] to logger [root].
log4net: Hierarchy Threshold []
log4net: Configuring Repository [log4net-default-repository]
log4net: Configuration update mode [Merge].
log4net: Logger [root] Level string is [ALL].
log4net: Logger [root] level set to [name="ALL",value=-2147483648].

log4net: Loading Appender [EventLogAppender] type: [Log4Mongo.MongoDBAppender, Log4Mongo]
log4net: Setting Property [ConnectionString] to String value [mongodb://username:password@localhost:27017/log]
log4net: Setting Property [CollectionName] to String value [events]
log4net: Setting Property [Name] to String value [timestamp]
log4net: Setting Property [Layout] to object [log4net.Layout.RawTimeStampLayout]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [hostname]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [ConversionPattern] to String value [%property{log4net:HostName}]
log4net: Converter [property] Option [log4net:HostName] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [Layout] to object [log4net.Layout.Layout2RawLayoutAdapter]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [invoker]
log4net: Setting Property [Key] to String value [invoker]
log4net: Setting Property [Layout] to object [log4net.Layout.RawPropertyLayout]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [level]
log4net: Parameter [layout] specified subtype [log4net.Layout.PatternLayout]
log4net: Converter [level] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Performing additional conversion of value from [PatternLayout] to [IRawLayout]
log4net: Setting Property [Layout] to Layout2RawLayoutAdapter value [log4net.Layout.Layout2RawLayoutAdapter]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [thread]
log4net: Parameter [layout] specified subtype [log4net.Layout.PatternLayout]
log4net: Converter [thread] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Performing additional conversion of value from [PatternLayout] to [IRawLayout]
log4net: Setting Property [Layout] to Layout2RawLayoutAdapter value [log4net.Layout.Layout2RawLayoutAdapter]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [message]
log4net: Parameter [layout] specified subtype [log4net.Layout.PatternLayout]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Performing additional conversion of value from [PatternLayout] to [IRawLayout]
log4net: Setting Property [Layout] to Layout2RawLayoutAdapter value [log4net.Layout.Layout2RawLayoutAdapter]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Setting Property [Name] to String value [ctx]
log4net: Setting Property [Key] to String value [ctx]
log4net: Setting Property [Layout] to object [log4net.Layout.RawPropertyLayout]
log4net: Setting Collection Property [AddField] to object [Log4Mongo.MongoAppenderFileld]
log4net: Created Appender [EventLogAppender]

log4net: Adding appender named [EventLogAppender] to logger [root].
log4net: Hierarchy Threshold []

Any thoughts? Thanks.

serenata-evaldas commented 8 years ago

did you try putting both of the appenders under a single log4net element? I have it configured like that but one logs to the file instead of mongo. So in your case it would be: