Link to home
Create AccountLog in
Avatar of computerstreber
computerstreberFlag for United States of America

asked on

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.CreateEventSource(sSource,sLog);
 
			EventLog.WriteEntry(sSource,sEvent);
			EventLog.WriteEntry(sSource, sEvent,
				EventLogEntryType.Warning, 234);
		}
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nhenny2009
nhenny2009

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer