- January 2006 (1)
- January 2007 (1)
- July 2007 (8)
- August 2007 (3)
- September 2007 (3)
- October 2007 (2)
- November 2007 (3)
- January 2008 (5)
- February 2008 (4)
- March 2008 (1)
- April 2008 (5)
- June 2008 (3)
- July 2008 (2)
- August 2008 (1)
- September 2008 (6)
- November 2008 (3)
- December 2008 (1)
- January 2009 (4)
- March 2009 (1)
- April 2009 (14)
- May 2009 (9)
- June 2009 (7)
- July 2009 (6)
- August 2009 (4)
- September 2009 (4)
- October 2009 (2)
- November 2009 (23)
- December 2009 (23)
- January 2010 (4)
- February 2010 (3)
- March 2010 (2)
- May 2010 (3)
- July 2010 (4)
.NET Event Logging
Thu, 09/20/2007 - 14:49 | by Kris
(Nobody else will care about this. Move along. Nothing to see here now. Maybe I'll clean this up for public consumption later.)
After spending way too much time figuring out how to change the name of a custom event log my .NET-based service was writing messages to, I decided I need to save the snippet of code that finally worked.
For more info about the confusing world of .NET's EventLog class, see http://www.informit.com/guides/printerfriendly.aspx?g=dotnet&seqNum=238.
namespace BlahBlahBlah
{
class Log
{
// Lots of stuff left out here.
// ...
private static EventLog eventLog = null;
private static readonly string eventSource = "My Service";
private static readonly string eventLogName = "My Log";
private static readonly string eventLogMachineName = "."; // local
///
/// Initializer for Log class
///
static Log()
{
// Register this event source if necessary
try
{
bool needCreate = false;
if (EventLog.SourceExists(eventSource))
{
string logName = EventLog.LogNameFromSourceName(eventSource,
eventLogMachineName);
if (logName != eventLogName)
{
EventLog.DeleteEventSource(eventSource);
needCreate = true;
}
}
else
{
needCreate = true;
}
if (needCreate)
{
EventLog.CreateEventSource(eventSource, eventLogName);
}
}
catch
{
// Ignore failure
}
// Initialize our EventLog instance
try
{
eventLog = new EventLog(eventLogName, eventLogMachineName, eventSource);
// Ensure our event log has the "overwrite as needed" setting
eventLog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded,
eventLog.MinimumRetentionDays);
}
catch
{
// Ignore failure
}
}
// Lots more stuff left out
// ...
}
}
Recent comments
5 weeks 5 days ago
6 weeks 2 days ago
7 weeks 6 days ago
8 weeks 3 days ago
8 weeks 3 days ago
9 weeks 1 day ago
11 weeks 4 days ago
12 weeks 2 days ago
15 weeks 1 day ago
17 weeks 4 days ago