• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1193
  • Last Modified:

C# Write to Event Log

The attached is code is used to write an event to the event log, i.e. Application Log. However, it doesn't include the user name that was executed the command. The user name just says N/A. I can include this:

sEvent = "Sample Event " + WindowsIdentity.GetCurrent().Name;

in the message, but is there a way to have it included in the event log itself?
using System;
using System.Diagnostics;
namespace WriteToAnEventLog_csharp
	/// Summary description for Class1.
	class Class1
		static void Main(string[] args)
			string sSource;
			string sLog;
			string sEvent;
			sSource = "dotNET Sample App";
			sLog = "Application";
			sEvent = "Sample Event";
			if (!EventLog.SourceExists(sSource))
			EventLog.WriteEntry(sSource, sEvent,
				EventLogEntryType.Warning, 234);

Open in new window

1 Solution
I don't think this is possible.  The user that is recorded in the event log is the username associated with the current running thread. I'm don't think there's a way to do this directly through the .NET Framework, and I believe that it is probably by design.  The best workaround would be to write the username you'd like into the message portion of your event entry.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now