I'm writing to the Application event log from an ASP.NET webapp. How can I populate the "User" field of the log entry?
Asked
Active
Viewed 1,091 times
2 Answers
1
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:
This is a non-trivial area, and not a case of simply supplying user details to the event logging call.
-
Any explanation for why I might see "N/A" in the User field of all entries created by my ASP.NET webapp (not using impersonation)? – lance Nov 22 '10 at 15:22
-
1The user is displayed as "N/A" when the thread is identified as running under the System account. – Tim Lloyd Nov 22 '10 at 15:36
-
I see. Seeing the "N/A" value made me think I could put a free-form text value in that field (if only I knew how). Reading the duplicate question to which Zhaph referred us above, I see that it must be an actual user on the box (which makes sense, now). – lance Nov 22 '10 at 16:30
0
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);

MAW74656
- 3,449
- 21
- 71
- 118
-
1
-
This will add the username into the message text, but it will not populate the "User" **field** in the event log. – Zhaph - Ben Duguid Nov 22 '10 at 15:24
-
@lance - No, your not missing anything, I misunderstood the question. Although, now that I look at the method, there doesn't seem to be a way to pass the username as a paramter. – MAW74656 Nov 22 '10 at 15:33