C# 在运行时更改侦听器属性

C# 在运行时更改侦听器属性,c#,logging,enterprise-library,event-log,C#,Logging,Enterprise Library,Event Log,我正在使用entlib5。我有一个事件日志侦听器,其Source属性当前设置为我们的应用程序套件的通用名称 此配置文件与多个项目链接/共享(说来话长,不会更改) 我们仍然希望每个应用程序将其自己的源名称放在事件日志中,以唯一地标识它…如何在运行时更改源名称 我可以轻松地更改有关实际日志事件本身的任何内容,但我想更改每个应用程序的侦听器源属性 希望这是有道理的 更新: 这是我们正在使用的配置设置…这是我想在运行时更改的源属性 add name=“Formatted EventLog General

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

此配置文件与多个项目链接/共享(说来话长,不会更改)

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

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

希望这是有道理的

更新: 这是我们正在使用的配置设置…这是我想在运行时更改的源属性


add name=“Formatted EventLog General”type=“Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener,Microsoft.Practices.EnterpriseLibrary.Logging,版本=5.0.0.0,区域性=中性,PublicKeyToken=31bf3856ad364e35”listenerDataType=“Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData,Microsoft.Practices.EnterpriseLibrary.Logging,Version=5.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”formatter=“Text formatter”traceOutputOptions=“None”filter=“All”machineName=“.”source=“运行时更改”log=“应用程序“

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

Logger.Write("test 1", "AppTwo");
下面的示例创建了两个类别-AppOne、AppTwo。 然后我添加了两个跟踪侦听器—AppOneListener(绑定到AppOne类别)和AppTwoListener(绑定到AppTwo侦听器)

在源代码中,当您想要登录时,请指定类别

Logger.Write("test 1", "AppTwo");
以下是配置: 请看categorySources部分和listeners部分

 <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>

对不起,我想我没有解释清楚。我们正在使用事件日志侦听器,它与平面文件具有不同的属性。我们只想要一个听众。我们目前有31个应用程序使用相同的链接配置文件。我们不希望31个侦听器的源名称不同。它使配置文件变得很大(我们有很多其他的东西在里面)。