Skip to content

Instantly share code, notes, and snippets.

@krcourville
Created February 24, 2015 21:43
Show Gist options
  • Select an option

  • Save krcourville/369fbe0c60a0604d9960 to your computer and use it in GitHub Desktop.

Select an option

Save krcourville/369fbe0c60a0604d9960 to your computer and use it in GitHub Desktop.
Mongo log4net appender sample using nuget package log4mongo-net
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<connectionStrings>
<add name="mongolog" connectionString="{mymongoconnectionstring}"/>
</connectionStrings>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<log4net>
<appender name="MongoDBAppender" type="Log4Mongo.MongoDBAppender, Log4Mongo">
<!--
MongoDB database connection in the format:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
See http://www.mongodb.org/display/DOCS/Connections for connectionstring options
If no database specified, default to "log4net"
-->
<!--<connectionString value="mongodb://localhost" />-->
<!--
Name of connectionString defined in web/app.config connectionStrings group, the format is the same as connectionString value.
Optional, If not provided will use connectionString value
-->
<connectionStringName value="mongolog" />
<!--
Name of the collection in database
Optional, Defaults to "logs"
-->
<collectionName value="console_test" />
<field>
<name value="timestamp" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</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="logger" />
<layout type="log4net.Layout.PatternLayout" value="%logger" />
</field>
<field>
<name value="message" />
<layout type="log4net.Layout.PatternLayout" value="%message" />
</field>
<field>
<name value="mycustomproperty" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="mycustomproperty" />
</layout>
</field>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="MongoDBAppender" />
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
</configuration>
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator()]
namespace ConsoleApplication1
{
class Program
{
static ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
log.Info("TEST INFO");
log.Fatal("TEST FATAL");
}
}
}
@gokhanatan
Copy link
Copy Markdown

Hello,I have a problem. If collection exist in my mongodb, I catch error.
But If collection isn't exist in my mongodb , Insert log .
I sharing my error detail . I wait your help @krcourville

:ERROR [MongoDBAppender] ErrorCode: GenericFailure. Failed in DoAppend
System.AggregateException: One or more errors occurred. ---> System.FormatException: The GuidRepresentation for the reader is CSharpLegacy, which requires the binary sub type to be UuidLegacy, not UuidStandard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment