log4net的配置控制台应用程序 [英] log4net Configuration for console app

查看:344
本文介绍了log4net的配置控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以提出如何配置log4net的一个控制台应用程序?

can anyone suggest how to configure the log4net for an console app?

或者至少如何/在哪里赶的Application_Start 事件? (它接缝,有些来电都需要在这一刻)

Or at least how/where to catch the Application_Start event? (It seams that some calls are required at this moment)

在此先感谢!

推荐答案

您需要配置它的第一个记录器实例化之前。

You need to configure it before the first logger is instantiated.

要做到这一点:

  • 您的主类(Program.cs中)不应该有一个记录

  • Your main class (Program.cs) should not have a logger

主要方法不应引用有记录的所有类。

The main method should not reference any classes that have a logger.

您可以再在main方法配置log4net的。

You can then configure log4net in the main method.

另外,您可以使用一个包装类实例化记录仪,确保log4net的是创建一个记录器,之前配置,例如:

Alternatively you can use a wrapper class to instantiate loggers, that ensures log4net is configured before creating a logger, e.g.:

static class Log4NetHelper
{
    private static bool _isConfigured;

    static void EnsureConfigured()
    {
        if (!_isConfigured)
        {
            ... configure log4net here ...
            _isConfigured = true;
        }
    }

    public static ILog GetLogger(string name)
    {
        EnsureConfigured();
        log4net.ILog logger = log4net.LogManager.GetLogger(name);
        return logger;
    }
}

这篇关于log4net的配置控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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