[英]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. 我有一个Web应用程序,它与桌面应用共享一个域层。 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. 我想登录域层,我想知道用户做了什么,桌面应用程序很简单,我可以使用静态记录器记录用户信息,因为我只有一个用户登录,但是在Web应用程序中我可以同时有很多,所以我不能使用相同的方法。
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. 好吧,最后,我发现要解决这个问题的唯一方法是使用一个具有'ThreadStatic'成员的帮助程序/管理器,每个线程都会有所不同,因为每个并发的webrequest都将使用不同的线程进行管理。 I can assing the logget at the begining and later use it in the code.
我可以一开始就配置logget,以后再在代码中使用它。 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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.