C# .NET - Writing to custom event log.
Asked By Byron King
30-Apr-07 11:53 AM
Hi. I am attempting to write to a custom event log, but whenever I try, the message goes straight to the Application log. Here's the code:
[CODE]
if (EventLoggingEnabled)
{
EventLog log = new EventLog();
log.Source = "Custom Log Security Library";
log.WriteEntry("This is a test message.", EventLogEntryType.Information);
}
[/CODE]
I see my message correctly displayed in the App log, but I want it to go in my custom log. As for the registry, I have the source defined at:
HKLM/SYSTEM/CurrentControlSet/Services/EventLog/CustomLog/CustomSource.
Any idea what I'm missing here? Thanks.
Take a looky here
http://support.microsoft.com/kb/815314
in the "Create Custom Logs" section.
Byron King replied to Peter Bromberg

Peter,
Thanks for the response. Actually, the article you posted was the first place I looked when I first attempted to write to the event logs. The strange thing is that my event source is not getting "unbound" from the Application log no matter what I do. I've deleted the custom log, deleted the source, rebooted, etc. The code I use to create the custom source is:
[CODE]
if (EventLoggingEnabled)
{
string errorMessage = string.Format(Resources.Culture, Resources.TicketCreationFailure, instanceName);
string entryText = eventLogEntryFormatter.GetEntryText(errorMessage, e.ExceptionMessage, e.ErrorMessage);
if (!(EventLog.SourceExists(Resources.EventSource, System.Environment.MachineName)))
{
EventLog.CreateEventSource(Resources.EventSource, Resources.EventLog);
}
EventLog.WriteEntry(Resources.EventSource, entryText, EventLogEntryType.Information);
}
[/CODE]
Deleting the event source produces a message in the Application log like this:
The description for Event ID ( 0 ) in Source ( ADC .NET Security Library ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event: An error occurred in application WindowsAppHarness.vshost.exe in the Advertising.Security.
The authentication ticket was created for .
04/30/2007 4:10:02 PM
.
If I go into the registry and restore the missing source, I get a correct event log message, but still in the incorrect log. Any other ideas are greatly appreciated.
Custom Event logs
Have you tried using this:
EventLog.CreateEventSource(string source, string logname);
Greg replied to Byron King
Once you create an event source, you cannot reuse that event source name. The framework searches through all of the event sources on the machine, and if it finds one that matches the name, it uses it. To make the code work properly, you need to create a new event source.

but are done with programming here! Really from now on you wan�t have to code. The rest will by done by configuring and customizing. So lets configure the server. I you have to place web.config file inside. It will look like this: Collapse | Copy Code < configuration > < configSections > < section name = " nhibernate" type = " System.Configuration.NameValueSectionHandler, System, Version = 1.0.1.0 Database = testdb;Trusted_Connection = True;" / > < add key = " hibernate.connection.isolation" value = " ReadCommitted" / > < / nhibernate > < system.runtime.remoting > < application > < channels > < serverProviders > < formatter ref = " binary" / > < / serverProviders > < channel ref = " http" > < / channel > < / channels > < service > < wellknown mode = " SingleCall CustomerDao.rem" / > < wellknown mode = " SingleCall" type = " MvpSample.Data.OrderDaoNHibernate, MvpSample.Data" objectUri = " OrderDao.rem" / > < / service > < / application > < / system.runtime.remoting > < system.web > < customErrors mode = " Off" > < / customErrors > < compilation > < assemblies > < add assembly = " System.Runtime HI Here is .net remoting under IIS Here are the steps to create a server application and client application. Server Object • Create a new VB.NET or C# class library IServer and replace code in Class1.vb with the following. Compile the project to IServer.dll . Collapse | Copy Code
to prevent exceptions from happening. Avoid using exceptions to handle business logic conditions in your code. Having said this, the next step is "what to do with exceptions". All developers know that when an application is deployed, it's almost never perfect. Sooner or later, some user is going to information for us to thoroughly and easily nail down a problem in a production web application. And so we are forced to spend much more time than we should attempting to error along with all the details right down to the page and exact line of code where your "perfect app" well - - BLEW UP! Not only that, but you can easily wire the following is NOT my idea of a legitimately "handled" exception: Try ' your buggy spaghetti code here Catch End Try - - I really hope, if you have digested the message correctly, that with here, is the fact that you can invoke whatever logging and additional exception - handling code you wish from within Application_Error. You don't need to use fancy HttpModules. All you need to do is set a "using" or "Imports" reference to you ExceptionHandler class library in your Global.asax and call your method(s). In writing this ExceptionLogging class library, my initial objectives were: 1) It should be simple to write and easy to understand
be wired up. At the same time, I'm going to include self-installing Service code (via Craig Andera's original piece) as well as code from Neil Baliga that enables recovery and autostart info for a service installer, and in addition, I'll include some really useful (a - la Thomas the Tank Engine) code that let's you debug a service by having it run as an executable in need to be installed to be run! First, let's look at the "TestService" sample code (the actual Windows Service): namespace TestService { using System; using System.Collections; using System.ComponentModel; using line { #if ( ! DEBUG ) ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] {new TestService()}; ServiceBase.Run(ServicesToRun); #else / / debug code: allows the process to run as a non-service / / will kick off the service start Required method for Designer support - do not modify / / / the contents of this method with the code editor. / / / < / summary> private void InitializeComponent() { components = new Container(); this .ServiceName = "TestService"; } / / / <summary> / / / Clean up any Start(); } / / / <summary> / / / Stop this service. / / / < / summary> protected override void OnStop() { test.Stop() ; } } } The only notable code you see above is that in Main, we use the self-installing service arrangement, which your service with "MyService.Exe / install". Nice! Same with uninstall. We also have the debug code which as can be seen, will run the service from the IDE in debug mode
didn't have the need for it at the time, so I just "retired the code" along with a lot of other stuff that was cluttering up my hard drives when Debugger.Launch(); / / use this to help debug - attach to a running service eng.BeginMonitor(); EventLog.WriteEntry( "SiteMonitorService starting. " ); } protected override void OnStop() { } } } The above is pretty much the standard guts of is, after you finally found InstallUtil.exe, read all the switch info, made sure your environment variables were all set, and finally were ready to do it "by hand". That code is in the ProjectInstaller.cs class file: using System; using System.Collections; using System.ComponentModel TRUE" ) && IsErrorCondition ) { InsertDB(strSource + " Info: " + ex.Message ); } } } void writeToEventLog( string strResult) { try { if (!EventLog.SourceExists( "Application" )) { EventLog.CreateEventSource( "Application" , "Application" , "." ); } EventLog objLog = new EventLog(); string strLogName = "Application" ; string message = strLogName; objLog.Source = "WebSite Monitor" ; objLog.WriteEntry(strResult, EventLogEntryType.Error); objLog.Close(); objLog