1
是否有方法將用戶名或用戶id(或一些附加數據)與OnEntry/OnSuccess/OnException上的參數一起記錄在一起。日誌附加信息OnEntry或OnSuccess
我需要我的日誌記錄看起來像:
「......方法調用的方法名使用參數[參數1:值1,參數2:值2 ...]由用戶:[用戶名]」
謝謝。
是否有方法將用戶名或用戶id(或一些附加數據)與OnEntry/OnSuccess/OnException上的參數一起記錄在一起。日誌附加信息OnEntry或OnSuccess
我需要我的日誌記錄看起來像:
「......方法調用的方法名使用參數[參數1:值1,參數2:值2 ...]由用戶:[用戶名]」
謝謝。
下面的代碼是從Postsharp文檔站點取自Trace Sample有一些小的修改
using System;
using System.Diagnostics;
using System.Reflection;
using PostSharp.Aspects;
namespace Samples
{
[Serializable]
public sealed class TraceAttribute : OnMethodBoundaryAspect
{
// This field is initialized and serialized at build time, then deserialized at runtime.
private readonly string category;
// These fields are initialized at runtime. They do not need to be serialized.
[NonSerialized] private string enteringMessage;
[NonSerialized] private string exitingMessage;
// Default constructor, invoked at build time.
public TraceAttribute()
{
}
// Constructor specifying the tracing category, invoked at build time.
public TraceAttribute(string category)
{
this.category = category;
}
// Invoked only once at runtime from the static constructor of type declaring the target method.
public override void RuntimeInitialize(MethodBase method)
{
string methodName = method.DeclaringType.FullName + method.Name;
this.enteringMessage = "Entering " + methodName;
this.exitingMessage = "Exiting " + methodName;
}
// Invoked at runtime before that target method is invoked.
public override void OnEntry(MethodExecutionArgs args)
{
Trace.WriteLine(this.enteringMessage, this.category);
DisplayArgs(args);
}
// Invoked at runtime after the target method is invoked (in a finally block).
public override void OnExit(MethodExecutionArgs args)
{
Trace.WriteLine(this.exitingMessage, this.category);
DisplayArgs(args);
}
}
}
private void DisplayArgs(MethodExecutionArgs args)
{
var parameters = args.Method.GetParameters();
var arguments = args.Arguments;
var zipped = parameters.Zip(arguments, (f,s) => f.Name + ":" + s == null ? "null" : s.ToString());
string traceLine = string.Format("invoked with params [{0}] by User:[{1}]", string.Join(",", zipped),
System.Security.Principal.WindowsIdentity.GetCurrent().Name);
System.Diagnostics.Trace.TraceInformation(traceLine);
}
其實這正是我需要的... http://stackoverflow.com/questions/12641871/using- postsharp與 - traceattribute換記錄與 - httpcontexts-的sessionid?RQ = 1 – user1376929