Solved

C# Write to Event Log

Posted on 2009-05-08
1
1,183 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:computerstreber
1 Comment
 
LVL 3

Accepted Solution

by:
nhenny2009 earned 250 total points
Comment Utility
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.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now