ASP.NET event log: populate "User" field?
I'm writing to the Application event log from an ASP.NET webapp. How can I开发者_如何学编程 populate the "User" field of the log entry?
The user that is recorded in the event log entry is the user that "owns" the thread (from a security perspective) at the time the event logging call was made. In an ASP.Net application (by default) this will be the account that ASP.Net is running under.
You can change which user a thread is running under using windows impersonation. For an example question on SO, see:
Windows Impersonation from C#
This is a non-trivial area, and not a case of simply supplying user details to the event logging call.
I grabbed the Username from Asp.net profile. Then I passed it into the method. Here I've used it in Global.asax, but the same principal should work elsewhere.
String username = User.Identity.Name;
String pageRequested = ((HttpApplication)sender).Request.Path;
error = Server.GetLastError().GetBaseException();
String message = "ERROR MESSAGE: " + error.Message +
"\nINNER EXCEPTION: " + error.InnerException +
"\nSOURCE: " + error.Source +
"\nFORM: " + Request.Form.ToString() +
"\nQUERYSTRING: " + Request.QueryString.ToString() +
"\nTARGETSITE: " + error.TargetSite +
"\nSTACKTRACE: " + error.StackTrace +
"\nNCLWEB USERNAME: " + username +
"\nDATESTAMP: " + DateTime.Now;
System.Diagnostics.EventLog.WriteEntry("NCLWeb", message, System.Diagnostics.EventLogEntryType.Error);
精彩评论