问题描述
我使用的 TraceAttribute
PostSharp每个类,以写入日志文件。一个新的要求是要能够prePEND每个日志条目的的SessionID
。
I'm using PostSharp with the TraceAttribute
on each class in order to write to log files. A new requirement is to be able to prepend each log entry with the SessionID
.
PostSharp似乎并不能够工作,虽然这种方式。我已经试过了是有道理的,我既不工作的两种方法。
PostSharp doesn't seem to be able to work this way though. I've tried the two approaches that made sense to me and neither work.
尝试#1
我添加了一个字符串参数传递给 TraceAttribute
构造函数传递并通过 OnEntry
,<$使用C $ C> OnExit 和 onException的
方法。
I added a string parameter to the TraceAttribute
constructor to be passed in and used by the OnEntry
, OnExit
and OnException
methods.
private readonly string _sessionID;
public TraceAttribute(string sessionID)
{
_sessionID = sessionID;
logger = new Logger();
}
public override void OnExit(MethodExecutionArgs args)
{
logger.LogMethodExit(string.Format("Session {0} Exiting {1}.{2}", _sessionID, args.Method.DeclaringType.FullName, args.Method.Name));
}
我的控制器都是用[追踪]属性原来的装饰,我修改,以这种方法...
My controllers were decorated with the [Trace] attribute originally and I modified to this approach...
[Trace(HttpContext.Session.SessionID)]
这不能作为工作的HttpContext
不是一个静态的情况下可用。
This doesn't work as the HttpContext
is not available in a static context.
这样的想法是窗外。
尝试#2
所以,我的下一个尝试是简单地在本地创建的sessionId
变量中的 TraceAttribute
每种方法...
So my next try was to simply create the sessionId
variable locally to each method in the TraceAttribute
...
public override void OnEntry(MethodExecutionArgs args)
{
var sessionID = HttpContext.Current.Session.SessionID;
logger.LogMethodEntry(string.Format("Session {0} Entering {1}.{2}", sessionID, args.Method.DeclaringType.FullName, args.Method.Name));
}
这不工作无论是作为再次,在的HttpContext
尚不可用,并在空
对象运行期间只要我运行的解决方案。
This doesn't work either as again, the HttpContext
is not yet available and is a null
object at runtime as soon I run the solution..
尝试#3
public override void OnEntry(MethodExecutionArgs args)
{
string sessionID = "Not initialized";
if (HttpContext.Current.Session.SessionID != null)
sessionID = HttpContext.Current.Session.SessionID;
logger.LogMethodEntry(string.Format("Session {0} Entering {1}.{2}", sessionID, args.Method.DeclaringType.FullName, args.Method.Name));
}
有没有什么办法让会话信息成 TraceAttribute
就是以这种方式与编织PostSharp?一些变通方法,可以完成这个目标?
Is there any way to get the session information into a TraceAttribute
that is weaved with PostSharp in this manner? Some workaround that can accomplish this goal?
推荐答案
得到它的工作,我错过了我会被关在web.config中。
Got it working, I was missing my session being turned in in web.config.
添加此行的配置,现在工作得很好。
Added this line to config and it works fine now.
<sessionState mode="InProc" cookieless="false" timeout="20"/>
这篇关于使用PostSharp与TraceAttribute与HttpContext的的SessionID的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!