Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# Write to Event Log

Posted on 2009-05-08
1
1,186 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

861 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