We are using log4net and want to specify it\'s configuration in an external config file (as we have done with other sections). To do this we have changed the log4net section
Do you have the following attribute in your AssemblyInfo.cs
file:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
and code like this at the start of each class that requires logging functionality:
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
I have a blog post containing this and other info here.
It is also possible to turn on a Debug mode for log4net
. Insert this into the App.config file:
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="link\to\your\file.log" />
</listeners>
</trace>
</system.diagnostics>
and you will at least get error messages, from which you can derive what exactly went wrong. In my case I simply forgot to set Copy to output directory
to Copy Always
.
I had the same problem, besides having
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net")]
in the running project, I also had the following line in a reference project:
[assembly: log4net.Config.XmlConfigurator]
When I removed this line in the referenced projects, the logs start to appear.
Make sure that your log4net.config file is set with the following properties:
Build Action: Content
Copy to output directory: Copy Always
In my case I got this error message:
log4net: config file [C:\........\bin\Debug\log4net.config] not found.
And log4net.config
the file was in Visual Studio 2017 project, but when I checked the file in bin\Debug
folder I could not find it.
My original assembly setup was like this:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
After some research time, I change it to the following and it works:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"..\..\log4net.config", Watch = true)]
Assuming you had an external config file called log4net.config that is copied to your deploy directory you can configure it like so:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;
using log4net;
namespace MyAppNamespace {
static class Program {
//declare it as static and public so all classes in the project can access it
//like so: Program.log.Error("got an error");
public static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
//configure it to use external file
log4net.Config.XmlConfigurator.Configure(new Uri(Application.StartupPath + "\\log4net.config"));
//use it
log.Debug("############# STARING APPLICATION #################");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
}