How configure log4net for a c# Windows service

家住魔仙堡 提交于 2019-12-03 06:01:28

问题


I have a windows service where the app.config file and the log4net.config files are seperate. The logging is not working.

In my app.config file, I have the following in the section:

<add key="log4net.Config" value="log4net.config"/>

This works if in the value I specify an absolute path to the log4net.config file.

Do I need to supply an absolute path to the log4net.cong? Both the log4net.config and app.config file are in the same folder as the executable.

any ideas?

Parag


回答1:


Maybe this is helpful (Moving your Log4Net configuration out of web.config with ASP.NET 2.0 web sites.).

Here are the parts that you might be interested:

Tell the application to configure log4net using this config file. There are really two spots for this. First, the global.asax and second the assemblyInfo.cs file. Note, that most of the time you will start out with a global.asax file with all of the code inline. For whatever reason, the only way I could get this to work was to break the global.asax up to use a code-behind and then ass the assemblyInfo.cs file. So it ends up looking like this.

global.asax: <%@ Application Language="C#" Inherits="GlobalAsax" %> global.asax.cs (in your App_Code folder):

using System;
using System.Web;

public class GlobalAsax : HttpApplication
{
    // you may have lots of other code here
    void Application_Start(object sender, EventArgs e)
    {
        log4net.Config.XmlConfigurator.Configure();
    }
}

Now that you have your application calling log4net's configuration, you can set an attribute in your assembly info so that log4net knows where to look for the configuration file. AssemblyInfo.cs (in your App_Code folder):

[assembly:log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

The watch flag tells log4net to keep an eye on the configuration file for potential changes. This is helpful if you want to change the configuration from logging everthing to errors only during the middle of your testing.

To make sure that the log4net.config is always there, add the log4net.config to the csproject, and set it to CopyAlways. It should be presented in the debug folder.

You can use a script to copy everything to a release folder, or if you are using MS setup project, manually add that file into the project. A reference will then be added.




回答2:


For Windows service the current directory is Windows\system32 -- and XmlConfigurator.Configure() uses FileInfo which treats relative paths relative to the current directory. So log4net is looking for its config file in Windows\system32.

XmlConfiguratorAttribute on the other hand uses application's BaseDirectory, this is why it always works.

Alternatively, you can do following before calling XmlConfigurator.Configure() :

System.IO.Directory.SetCurrentDirectory (AppDomain.CurrentDomain.BaseDirectory);


来源:https://stackoverflow.com/questions/1605765/how-configure-log4net-for-a-c-sharp-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!