MichaelD
MichaelD

Reputation: 8767

How to direct the EventLogTraceListener to create in a specific Log

The following listener will create an event entry when the Trace.WriteLine is called. If the source does not exist he will create it in the default log channel which is 'Application' . I want to specify another default Log channel but after searching for 45 minutes i don't seem to find the solution. Any ideas?

<configuration>   
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="myListener"               
          type="System.Diagnostics.EventLogTraceListener"
          initializeData="Source">          
        </add>
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

Upvotes: 7

Views: 10342

Answers (4)

Luke
Luke

Reputation: 949

Just had this problem myself and found a solution that doesn't require custom code - you can create the "Source" beforehand via Powershell and that will let you set what log to use.

  1. Initialize source via Powershell:
# Run as administrator

# ONLY If you previously ran the application and the source is already associated with the Application channel:
Remove-EventLog -Source MyCustomSource

# Create new log channel <-> source mapping
New-EventLog -LogName CustomLog -Source MyCustomSource
  1. Restart the machine if you are using the same "Source" you previously logged to "Application" - changes won't apply until reboot.

  2. Match "Source" in the diagnostics XML with newly created source

<configuration>   
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="myListener"               
          type="System.Diagnostics.EventLogTraceListener"
          initializeData="MyCustomSource">          
        </add>
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

Upvotes: 0

John
John

Reputation: 1247

You could repoint the listener in the first line of code.

Trace.Listeners["MyListener"].Attributes["EventLog"] = ConfigurationManager.AppSettings["MyCustomEventLogName"];

The value can be stored in the <appSettings> section of the config file so it's still configuration based:

<appSettings>
    <add key="MyCustomEventLogName" value="CustomEventLogName" />
</appSettings>

Upvotes: 0

ErickOrlando
ErickOrlando

Reputation: 1

You can find a solution for this in this blog post:

http://weblogs.asp.net/psteele/438936

It really works!

Upvotes: -1

Brian Rudolph
Brian Rudolph

Reputation: 6318

Not sure that you can via the config.

Though the EventLogTraceListener does accept a different eventlog as a parameter in the constructor. Unfortunately, the class is sealed so you can't simply derive from it and pass a different value for the constructor.

Though you could follow this approach and construct your own class(seems fairly simple). And then reference that type in your config. http://weblogs.asp.net/psteele/archive/2006/02/23/438936.aspx

Upvotes: 3

Related Questions