Here are the basic steps in getting this working:
1. Setup logging configuration
2. Create the event source before running your app.
1a. You need to add the Event log listener under the listeners node:
<listeners>
<add name="Event Log Listener"
formatter="Text Formatter"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
source="SourceNameCustomize" machineName="." log="Application"/>
1b. Next under categorySources node, add a reference to the Event Log Listener defined above, like so:
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Event Log Listener"/>
</listeners>
</add>
Now your app is ready to publish events to the Events log.
2. But before you actually do, you need to create the EventSource and as this needs elevated privileges, you should do it using a special tool or do it as part of the setup/deployment process.
Here is the code I typically use to create the eventSource. (I build it into a command line utility that can be run on the machine).
using System;
public class EventSourceCreatorUtility
{
public static void RunSnippet(string logName, string eventSource)
{
if (!EventLog.SourceExists(eventSource))
{
System.Diagnostics.EventLog.CreateEventSource(eventSource, logName);
Console.WriteLine("Event Log and Source: {0},{1} were created successfully.",logName, eventSource);
}
else
{
Console.WriteLine("Event log/source: {0}/{1} already exists",logName, eventSource);
}
Console.WriteLine("Done");
}
public static void Main(string[] args)
{
try
{
if (args != null && args.Length == 2)
{
RunSnippet(args[0], args[1]);
}
else
{
Console.WriteLine("Parameters: logName, eventSource [eg: Application, AppName]");
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.ToString());
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
}
No comments:
Post a Comment
Remember, if you want me to respond to your comment, then you need to use a Google/OpenID account to leave the comment.