语义记录到多个文件

时间:2015-10-29 08:21:34

标签: logging semantic-logging

我需要将我的网络应用程序错误记录到三个不同的文件

对于Ex: -

     Dashboard --> DashboardLog.Log

     Login Information --> LoginLog.Log 

     AAA Section --> AAALog.Log

我尝试根据“LogToRollingFlatFile”方法中的my类型在运行时更改fileName。但是当我再次刷新页面说它被另一个进程使用时,它会抛出一个错误。它似乎没有释放以前的日志资源,当它再次尝试写入时会抛出此错误。贝娄是我改变道路的方法,

public static void ChangePath(int typeId)
    {
        var listener = new ObservableEventListener();
        listener.DisableEvents(SemanticLoggingEventSource.Log);
        SemanticLoggingEventSource.Log.Dispose();

        listener.EnableEvents(
          SemanticLoggingEventSource.Log, EventLevel.LogAlways,
          SemanticLoggingEventSource.Keywords.Perf | SemanticLoggingEventSource.Keywords.Diagnostic);

        if (typeId == 1)
        {
            listener.LogToRollingFlatFile(
            ConfigurationManager.AppSettings["DashboardLogLocation"].ToString(),
            10,
            "dd-M-yyyy",
            RollFileExistsBehavior.Increment,
            RollInterval.Hour,
            null,
            0,
            true);
        }
        else if (typeId == 2)
        {
            listener.LogToRollingFlatFile(
            ConfigurationManager.AppSettings["LoginLogLocation"].ToString(),
            10,
            "dd-M-yyyy",
            RollFileExistsBehavior.Increment,
            RollInterval.Hour,
            null,
            0,
            true);
        }
        else
        {
            listener.LogToRollingFlatFile(
            ConfigurationManager.AppSettings["AAALogLocation"].ToString(),
            10,
            "dd-M-yyyy",
            RollFileExistsBehavior.Increment,
            RollInterval.Hour,
            null,
            0,
            true);
        }
    }

这就是它将被调用的方式,

        SemanticLoggingUtility.ChangePath(1);
        EventLogger.ErrorLog("Dashboard Error", "test description 1");
        SemanticLoggingUtility.ChangePath(2);
        EventLogger.ErrorLog("Login Error", "test description 2");

1 个答案:

答案 0 :(得分:0)

我知道这可能不是您正在寻找的那种答案,但对于这种特殊情况,我认为拥有多个文件接收器将是最合适(和解耦)的解决方案。

只是强制执行之前的声明,使用不同的记录器它也是一种常见的做法(由Microsoft ILoggerFactory完成,在不同的类中提供不同的记录器)。