在运行时更改侦听器属性

时间:2011-02-11 05:16:12

标签: c# logging enterprise-library event-log

我正在使用Ent Lib 5.我有一个事件日志侦听器,其Source属性当前设置为我们的应用程序套件的通用名称。

此配置文件与多个项目链接/共享(长篇故事,不会更改)。

我们仍然希望每个应用程序在事件日志中放置自己的Source名称以唯一标识它...如何在运行时更改源名称?

我可以轻松地更改我想要的实际日志事件本身,但我想更改每个应用程序的侦听器源属性。

希望这是有道理的

更新: 这是我们正在使用的配置设置......它是我想在运行时更改的SOURCE属性。

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 “machineName =”。“ source =“在运行时更改”log =“应用程序”

1 个答案:

答案 0 :(得分:0)

您可以要求记录器写入特定类别。

下面的示例,创建了2个类别 - AppOne,AppTwo。 然后我添加了两个跟踪侦听器 - AppOneListener(绑定到AppOne类别)和AppTwoListener(绑定到AppTwo Listener)。

在源代码中,如果要记录,请指定类别。

Logger.Write("test 1", "AppTwo");

以下是配置: 查看categorySources部分和监听器部分。

 <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}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;"
                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 &amp; Warnings" />
        </specialSources>
    </loggingConfiguration>
相关问题