在运行时更改侦听器属性

发布于 2024-10-17 02:11:07 字数 799 浏览 8 评论 0原文

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

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

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

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

希望这是有道理的

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

添加名称=“格式化的EventLog常规”类型=“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、版本=5.0.0.0、文化=中性、PublicKeyToken=31bf3856ad364e35"formatter="文本格式化程序"traceOutputOptions="无"filter="全部"machineName= “。”源=“运行时更改”日志=“应用程序”

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" machineName="." source="CHANGE AT RUNTIME" log="Application"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间不值得 2024-10-24 02:11:07

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

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

当你想记录日志时,在源代码中指定类别。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文