janitheshan
janitheshan

Reputation: 325

Semantic Logging to multiple files

I need to log my web application errors to three different files

For Ex:-

     Dashboard --> DashboardLog.Log

     Login Information --> LoginLog.Log 

     AAA Section --> AAALog.Log

I have tried to change the fileName at the run time according to the my type from "LogToRollingFlatFile" method. But it throws an error when I refresh the page again saying the it is used by another process. It seems like it hasn't released the previous time log resources and when it tries to write again it throws this error. Bellow is the my path changing method,

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);
        }
    }

and this is how it's gonna be invoked,

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

Upvotes: 0

Views: 173

Answers (1)

mekoda
mekoda

Reputation: 333

I know this probably is not the kind of answer you are looking for, but for this particular case I think having multiple file sinks will be the most suitable (and decoupled) solution.

Just to enforce the previous statement, having different loggers it's also a common practice (as done by Microsoft ILoggerFactory, providing different loggers across different classes).

Upvotes: 0

Related Questions