Log4Net - log file per user logged in.

0
0

Hi,

We use Log4Net for all of our logging and we have an issue in WiseJ and I’m thinking it could be to do with threads. When a user logs into our application, we setup a log file for them with their username in the filename. We then use Logging.Info / Error to log to that file. The issue we have is that log messages are going to the wrong files a lot.

We either need to resolve this or change the logging tool we use (would love to hear what other people use). Here’s our Logging class:

public class Logging
{
    private static readonly ILog log = LogManager.GetLogger(typeof(Logging));

    public static void Setup(string userName)
    {
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

        hierarchy.Root.RemoveAllAppenders();

        PatternLayout patternLayout = new PatternLayout();
        patternLayout.ConversionPattern = "%date{dd-MMM-yyyy HH:mm:ss.fff} %-5level - %message%newline";
        patternLayout.ActivateOptions();

        FileAppender fileAppend = new FileAppender();
        fileAppend.AppendToFile = true;
        fileAppend.File = $"LogFile-{userName}.log";
        fileAppend.Layout = patternLayout;
        fileAppend.ActivateOptions();
        hierarchy.Root.AddAppender(fileAppend);

        ConsoleAppender consAppender = new ConsoleAppender();
        consAppender.Layout = patternLayout;
        consAppender.ActivateOptions();
        hierarchy.Root.AddAppender(consAppender);

        hierarchy.Root.Level = Level.Info;
        hierarchy.Configured = true;
    }

    public static void Info(string infoMessage)
    {
        log.Info(infoMessage);
    }

    public static void Error(string errorMessage)
    {
        log.Error(errorMessage);
    }

    public static void Error(string errorMessage, Exception ex)
    {
        log.Error(errorMessage, ex);
    }
}

 

  • You must to post comments
Showing 0 results
Your Answer

Please first to submit.