How to direct the EventLogTraceListener to create in a specific Log
Asked Answered
F

4

7

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>
Fabiolafabiolas answered 15/7, 2009 at 20:23 Comment(0)
M
3

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

Madid answered 15/7, 2009 at 20:50 Comment(1)
This doesn't address the OPs question. The OP would like to log the events to a different location (e.g. a folder under Applications and Services logs). All this code does is show you how to filter to a specific logging type, which is already handled.Shemeka
S
0

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>
Spy answered 3/8, 2017 at 10:0 Comment(0)
V
0

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>
Video answered 25/5, 2021 at 15:46 Comment(0)
S
-1

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

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

It really works!

Simard answered 16/3, 2015 at 16:41 Comment(1)
You should put the essential parts in your answer post and add the link as a reference.Exuberate

© 2022 - 2024 — McMap. All rights reserved.