简体   繁体   中英

asp.net logger userinfo per web request

Hope I explain my problem well.

I have a web application which share a domain layer with a desktop aplication. You can have the application as a website or as desktop application, but the domain logic is the same, so both share that piece of code.

I want to log the domain layer and I want to know what the user have done, with the desktop application is easy, I can use a static logger with the user info in it, as I have just one user logged, but in the webapplication I can have many at the same time so I can't use the same approach.

I would like to avoid to pass the user info in every call of the domain layer.

Thanks

Well, at the end the only way I found to aproach this is using a helper/manager with a 'ThreadStatic' member, which will be different per thread, as each concurrent webrequest will be managed with a diferent thread. I can assing the logget at the begining and later use it in the code. Hope someone can find a better solution.

public static class LoggerHelper
{
    [ThreadStatic]
    static public ILogger _logger;
    static public ILogger Logger
    {
        get { return _logger; }
        set { _logger = value; }
    }
}

In web service:

public bool TestLogger()
{
//initialize logger.
AmfContext.Logger = _logger = new WebLogger();
//call internal logic
.............
}

In code:

LoggerHelper.Logger.Info("Log line example");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM