Thursday, 8 August 2013

Can't load Log4Net configuration

Can't load Log4Net configuration

I have a brand new console application in vb.net. I want to use log4net so
I did the following steps and it works. Great. Yahoo.
But I have to place Log4Net.config in bin/debug together with Log4Net.dll
and Log4Net.xml. I have tried many things but no joy. Or I haven't got to
a right combination. How can i move Log4Net.config to app root?
Installed Log4Net from NuGet.
I added
<Assembly: XmlConfigurator(ConfigFile:="Log4Net.config", Watch:=True)>
in AssemblyInfo.vb.
This is how i am calling it.
Public Class Class1
'Save log4net log into SQL Server
Private Shared ReadOnly DBlog As ILog = LogManager.GetLogger("TestLog4Net")
Public Shared Sub Main(ByVal args() As String)
DBlog.Error("Log4Net testing v1")
End Sub
End Class
My Log4Net.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="xxxxx" />
<commandText value="xxxxx" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
I have tired
to set the following line before I say DBlog.Error("xx")
log4net.Config.XmlConfigurator.Configure()
to set this in app.config.
<log4net configSource="Log4Net.config" />
to move the whole Log4Net.config to app.config. That didn't work.
to set this as someone suggested on one post. That didn't work either.
<appSettings>
<add key="log4net.Config" value="log4net.config"/>
<add key="log4net.Config.Watch" value="True"/>
</appSettings>
to declare this instead of the GetLogger("name") that i am using. No joy
either.
Private Shared ReadOnly log As ILog =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)

No comments:

Post a Comment