log4net的从外部配置文件设置为不工作 [英] log4net set from an external config file not working

查看:192
本文介绍了log4net的从外部配置文件设置为不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与log4net的一个奇怪的问题。

I'm having a weird issue with log4net.

在ASP.NET应用程序中,我要在外部配置log4net的,所以我有我挂钩到我的web应用程序与这条线在属于我的web应用程序AssemblyInfo.cs文件单独log4net.config文件:

In an ASP.NET application, I want to configure log4net externally, so I have a separate log4net.config file which I hook up to my web app with this line in the AssemblyInfo.cs file belonging to my web app:

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

现在,如果我实例化log4net的记录器类这样的正常方式:

Now, if I instantiate the log4net logger class the normal way like this:

public class MyClass
{
    private static readonly ILog _logger = log4net.LogManager.GetLogger(typeof(MyClass));
    ....

那么这个作品,并记录工作正常。不过,我裹在LogManager类,它是一个独立的组件(基础设施)的一部分,我的记录code,并在一批项目被重用。它有一个GetLogger看起来像这样:

Then this works, and the logging works as normal. However, I've wrapped my logging code in a LogManager class, which is part of a separate assembly (Infrastructure), and is reused across a number of projects. It has a GetLogger that looks like this:

public static class LogManager
{
    public static ILog GetLogger()
    {
        var stack = new StackTrace();
        var frame = stack.GetFrame(1);
        return new log4net.LogManager.GetLogger(frame.GetMethod().DeclaringType);
    }
}

因此​​,我可以在我的asp.net code使用:

So I can use this in my asp.net code:

public class MyClass
{
    private static readonly ILog _logger = LogManager.GetLogger();
    ....

但是......这不行!不记录产生它似乎并不正确挂钩的配置文件。如果我把我的log4net的配置直接进入web.config中,那么这个日志管理工作正常。

But... This doesn't work! No logging is produced It doesn't seem to hook up the config file correctly. If I put my log4net config directly into the web.config, then this LogManager works fine.

推荐答案

要总结一下Bibhu说。它具有所有与做 log4net.Config.XmlConfigurator

To sum up what Bibhu has said. It has all to do with log4net.Config.XmlConfigurator.

有无的配置log4net的日志记录前将启动。所以每当你用你的LogManager类的托管应用程序(Windows,网络)的必须的配置日志。

You have to configure log4net before logging will start. So whenever you use your LogManager class the hosting application (windows, web) must configure the logging.

如果不是这样,你需要做的是在你的日志管理。

Either that or you need to do it in your LogManager.

这篇关于log4net的从外部配置文件设置为不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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