如何为C#Windows服务配置log4net [英] How configure log4net for a c# Windows service

查看:642
本文介绍了如何为C#Windows服务配置log4net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务,其中app.config文件和log4net.config文件是分开的.日志记录无法正常工作.

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

在我的app.config文件中,本节中包含以下内容:

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

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

如果在值中我指定了log4net.config文件的绝对路径,则此方法有效.

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

我是否需要提供log4net.cong的绝对路径? log4net.config和app.config文件都与可执行文件位于同一文件夹中.

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.

有什么想法吗?

Parag

推荐答案

也许这是有帮助的(使用ASP.NET 2.0网站将Log4Net配置从web.config中移出.)

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:

告诉应用程序进行配置 使用此配置文件的log4net.那里 确实有两个地方.第一的, global.asax,然后第二个 assemblyInfo.cs文件.注意,大多数 在这段时间里,您将以 带有所有代码的global.asax文件 排队.无论出于何种原因, 我可以使它工作的方式是 分解global.asax以使用 代码隐藏然后再把 assemblyInfo.cs文件.这样就结束了 看起来像这样.

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: <%@应用程序语言="C#" Inherits ="GlobalAsax"%> global.asax.cs(在您的App_Code文件夹中):

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();
    }
}

现在您有了自己的应用程序 调用log4net的配置,您 可以在程序集中设置属性 信息,以便log4net知道在哪里 查找配置文件. AssemblyInfo.cs(在您的App_Code中 文件夹):

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)]

watch标志告诉log4net保持 关注配置文件 潜在的变化.如果这是有帮助的 您想要更改配置 从一切都记录到错误 在测试过程中.

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.

要确保log4net.config始终存在,请将log4net.config添加到csproject中,并将其设置为CopyAlways.它应该显示在debug文件夹中.

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.

您可以使用脚本将所有内容复制到发行文件夹,或者,如果您正在使用MS安装项目,请手动将该文件添加到项目中.然后将添加参考.

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.

这篇关于如何为C#Windows服务配置log4net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆