C# 如何在 log4net appender 名称中使用 GlobalContext 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/562108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:15:39  来源:igfitidea点击:

How do I use a GlobalContext property in a log4net appender name?

c#log4net

提问by Long Pham

I'm trying to customise a log4net file path to use a property I have set in the log4net.GlobalContext.Propertiesdictionary.

我正在尝试自定义 log4net 文件路径以使用我在log4net.GlobalContext.Properties字典中设置的属性。

log4net.GlobalContext.Properties["LogPathModifier"] = "SomeValue";

I can see that this value is set correctly when debugging through it. and then in my configuration

通过它进行调试时,我可以看到该值设置正确。然后在我的配置中

<file type="log4net.Util.PatternString" 
      value="Logs\%appdomain_%property{LogPathModifier}.log" />

However, the output of this gives me "_(null).log" at the end of the path. What gives?

但是,它的输出在路径的末尾给了我“_(null).log”。是什么赋予了?

采纳答案by Dscoduc

I ran into the same behavior and solved it by setting the global variable before calling the XmlConfigurator... Here is what I am successfully using:

我遇到了相同的行为并通过在调用 XmlConfigurator 之前设置全局变量来解决它......这是我成功使用的:

log4net.config details:

log4net.config 详细信息:

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
  <File type="log4net.Util.PatternString" value="App_Data/%property{LogName}" />
  ...
</appender>

Global.asax details:

Global.asax 详细信息:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger("Global.asax");
void Application_Start(object sender, EventArgs e) 
{
    // Set logfile name and application name variables
    log4net.GlobalContext.Properties["LogName"] = GetType().Assembly.GetName().Name + ".log";
    log4net.GlobalContext.Properties["ApplicationName"] = GetType().Assembly.GetName().Name;

    // Load log4net configuration
    System.IO.FileInfo logfile = new System.IO.FileInfo(Server.MapPath("log4net.config"));
    log4net.Config.XmlConfigurator.ConfigureAndWatch(logfile);

    // Record application startup
    log.Debug("Application startup");
}

Hope this helps...

希望这可以帮助...

回答by Dillie-O

Has the logger been initialized through the global or main method in the application? It could be that the GlobalContext has not been initialize yet.

logger 是通过应用程序中的 global 还是 main 方法初始化的?可能是 GlobalContext 尚未初始化。

回答by Dscoduc

Add type=log4net.Util.PatternStringinto File element

将 type= 添加log4net.Util.PatternString到 File 元素中

回答by Dscoduc

The problem( I think) is that you GET(GetLogger) the logger before you set the name and load the config...

问题(我认为)是您在设置名称和加载配置之前获取(GetLogger)记录器...

Try to do declare the logger like: private static log4net.ILog _pLogand then in the Application_Start do:

尝试像这样声明记录器:private static log4net.ILog _pLog然后在 Application_Start 中执行:

void Application_Start(object sender, EventArgs e) 
{
    // Set logfile name and application name variables
    log4net.GlobalContext.Properties["LogName"] = GetType().Assembly.GetName().Name + ".log";
    log4net.GlobalContext.Properties["ApplicationName"] = GetType().Assembly.GetName().Name;

    // Load log4net configuration
    System.IO.FileInfo logfile = new System.IO.FileInfo(Server.MapPath("log4net.config"));
    log4net.Config.XmlConfigurator.ConfigureAndWatch(logfile);

    //Get the loger
    _pLog = log4net.LogManager.GetLogger("Global.asax");

    // Record application startup
    pLog .Debug("Application startup");
}

So the sequence is:

所以顺序是:

// Set logfile name and application name variables
// Load log4net configuration
// get the logger
// Record application startup