Change listener properties at run time
I am using Ent Lib 5. I have a Event Log Listener with the Source attribute currently set to a generic name for our suite of applications.
This configuration file is linked/shared with several projects (long story and will not change).
We would still like each application to put its own Source name in the event log to uniquely identify it...how do I go about changing the Source name at run time?
I can easily change anything I want about the actual log event itself but I want to change the listener source attribute for each app.
Hopefully this makes sense
UPDATE: Here is the config setting that we are using...it is the SOURCE attribute that I want to change at runtime.
add name="Formatted EventLog General" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" formatter="Text Formatter" traceOutputOptions="None" filter="All" machi开发者_开发技巧neName="." source="CHANGE AT RUNTIME" log="Application"
You can ask the Logger to write to a specific category.
The sample below, created a 2 categories - AppOne, AppTwo. And then I've added two trace listeners - AppOneListener (bound to AppOne category) and AppTwoListener (bound to AppTwo Listener).
And in source code when you want to log, specify the category.
Logger.Write("test 1", "AppTwo");
Below is the configuration: Look at the categorySources section and listeners section.
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="AppOne">
<listeners>
<add name="AppOneListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="traceAppOne.log" formatter="Text Formatter" />
<add name="AppTwoListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="traceAppTwo.log" formatter="Text Formatter" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="Title:{title}{newline}
App Domain: {localAppDomain}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="AppOne" />
<add switchValue="All" name="AppTwo">
<listeners>
<add name="AppTwoListener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings" />
</specialSources>
</loggingConfiguration>
精彩评论