简体   繁体   English

使用log4net的每条消息的新文件

[英]New file for each message with log4net

I'm using log4net to trace application errors and debugging information, all of that goes into one big file and is working fine. 我正在使用log4net跟踪应用程序错误和调试信息,所有这些都放入一个大文件中,并且工作正常。

But I'm also want to create a separate log4net appender which will be used to dump incoming messages to disk. 但是我还想创建一个单独的log4net附加程序,该附加程序将转储传入的消息到磁盘。 These will be in a separate directory and I want each call to log.Info to be written to a new file, preferably with a timestamp in the filename. 这些将位于单独的目录中,我希望每个对log.Info调用都被写入一个新文件,最好在文件名中带有时间戳。

That way I can easily bring up the specific message that was sent to my component at a given timestamp. 这样,我可以轻松调出在给定时间戳记下发送到我的组件的特定消息。

Can this be done through configuration? 可以通过配置完成吗?

Would something like this work for you? 这样的事情对您有用吗?

private void LogFile(string logFileName, string message, Exception ex)
{
    log4net.ThreadContext.Properties["Version"] = "1";
    log4net.GlobalContext.Properties["LogName"] = logFileName;

    ILog logger = LogManager.GetLogger("DeIdentifyDicom");

    log4net.Config.XmlConfigurator.Configure();

    if (ex != null)
    {
        logger.Fatal(message, ex);
    }
    else
    {
        logger.Fatal(message);
    }
}

how do I define log file at run time? 如何在运行时定义日志文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM