Solved

C# Write to Event Log

Posted on 2009-05-08
1
1,185 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
ID: 24341595
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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