Reputation: 7823
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>
</appender>
<logger name="TestLog4Net">
<level value="ALL" />
<appender-ref ref="AdoNetAppender" />
</logger>
</log4net>
</configuration>
I have tried
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)
Upvotes: 0
Views: 2953
Reputation: 18759
In my C# console application I have the following:
AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
App.Config
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<!--
This writes the log information to the console window. It only logs events
that are at least at the INFO level (which would mean that DEBUG events are not
captured.
-->
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<!--
This stores information in the mylogfile.txt file. It only captures log events
that contain the key word test or error.
-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs/file-"/>
<datePattern value="yyyy-MM-dd.lo\g"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="ConsoleAppender"/>
</root>
</log4net>
within my classes, I have..
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Log.InfoFormat("...");
Upvotes: 0
Reputation: 363
If you want to just move the Log4net.config file to bin/Debug, try right-clicking the file in Solution Explorer -> Properties -> Change the value of Copy to Output Directory to Copy Always. I'm not sure if this is what you want. So, I maybe wrong. Feel free to correct me. ;)
EDIT: Another way is to write a pre-build command. Right-click the project in Solution Explorer -> Properties -> Go to Build Events tab
Assuming the Log4net.config file is present in the project directory (directory where your vb files are present), the following command can be given under the Pre-build event command line,
xcopy "$(ProjectDir)Log4net.config" "$(TargetDir)"
Upvotes: 2