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
0
0

Hi Alaa,

I think we can use the ThreadContext in Log4Net. I just read this: .net – What is the difference between log4net.ThreadContext and log4net.LogicalThreadContext? – Stack Overflow.

I’d welcome your thoughts on this though.

Neil.

  • You must to post comments
0
0

Hi Alaa – once we’ve logged the user on, we call the Logging.Setup with the user’s email address. This is before any logging.info or logging.error call. The issue we have is that user’s logs are getting mixed up, even though a log file is always generated per user.

  • You must to post comments
0
0

Hi Neil,

Any chance you could tell me how this logger class is setup in your application?

Best Regards,
Alaa

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.