8

NOTE: I read this question and answer, and it does not work for what I want: Log4Net: Programmatically specify multiple loggers (with multiple file appenders)

I have a WCF service that is a "Question and Answer" style service. It gets inputs and sends outputs. It does not persist much at all.

I need to log each Question and Answer session in a separate file.

I have a single Appender (currently the RollingAppender).

Is there some way to start a new log file for each call to my WCF service?

NOTE: I am using an XML Layout, the idea is that the output of the log can be parsed and displayed graphically (a later feature). Kind of like a "Query Plan". This is another reason that I need them in a separate file.

NOTE: In case another reason is needed, the Log4Net XmlLayoutBase will not drop xml footers until the app closes. Which is not really a planned event for an WCF Service hosted in IIS.

4

3 回答 3

6

这似乎对我有用:

public static void StartNewFile(this ILog log, string newFileName)
{
    Logger logger = (Logger) log.Logger;

    while (logger != null)
    {
        foreach (IAppender appender in logger.Appenders)
        {
            FileAppender fileAppender = appender as FileAppender;
            if (fileAppender != null)
            {
                fileAppender.File = newFileName;
                fileAppender.ActivateOptions();
            }
        }
        logger = logger.Parent;
    }
}

它需要以下参考资料:

using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;
于 2013-04-30T17:33:05.160 回答
2

而不是记录到文件,也许您可​​以尝试记录到数据库表并使用记录的数据记录会话 ID。通过这种方式,您可以根据会话 ID 对表进行选择并仅查看其数据。

于 2013-04-30T16:39:15.623 回答
0

也许不是您正在寻找的确切解决方案,但您可以通过在每个接口调用开始时调用它来为每个会话创建不同的记录器:

ILog logger = LogManager.GetLogger(<SessionID>);

您将在同一个日志文件中获得所有条目,但是使用诸如log4view 之类的查看器分别查看每个会话非常容易。

希望能帮助到你

于 2014-03-26T15:05:26.997 回答