记录多实例应用程序最佳实践?

发布于 2024-11-02 04:41:59 字数 299 浏览 11 评论 0原文

我终于在我的 WPF 桌面应用程序中尝试了 log4net。

我正在努力解决 RollingFileAppender 没有对多实例应用程序的内置支持这一事实。

我不喜欢仅仅为了让记录器高兴而将应用程序限制为单个实例的想法。单实例技巧都是丑陋的技巧。

在日志文件的文件名中使用进程 ID 也不够好。这有可能会占用无限空间,因为 RollingFileAppender 在这种情况下毫无用处。

一种解决方案可能是将日志发送到不同的进程,该进程负责将输出序列化到文件中。但这又带来了新的麻烦。

您对此有何看法?

I finally tried log4net for my WPF desktop application.

I'm struggling with the fact that RollingFileAppender has no built in support for multiple instance application.

I don't like an idea of restricting the application to single instance just to make logger happy. Single intstance tricks are all ugly hacks.

Using process ID in the filename of the log file is also not good enough. This has potential of eating up unlimited space, since RollingFileAppender is useless in this situation.

One solution would probably be to send logs to different process, which would take care of serializing the output into files. But this creates new headaches.

What's your take on this?

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

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

发布评论

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

评论(4

撩发小公举 2024-11-09 04:42:00

使用来自不同进程的多个 RollingFileAppender 实例写入同一个文件并不是一个好主意,因为 RollingFileAppender 不是为这种情况设计的

您在这里有几个选择:

具有最小锁定的多个 FileAppender

使用多个 FileAppender指向同一文件并配置了最小锁定的实例。这将允许多个进程并发写入操作:

<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="Log.txt" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern
            value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
</appender>

多个 EventLogAppender

使用多个 EventLogAppender 实例写入共享 Windows 事件源:

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
    <applicationName value="MyApp" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern
            value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
</appender>

当然,您也可以将日志 发送到数据库到在不同进程中运行的远程组件或即使是不同的机器,但这些选择需要设置更多的基础设施,因此对于您的场景来说可能有点过分了。

相关资源:

It's not a good idea to use multiple RollingFileAppender instances from different processes writing to the same file since the RollingFileAppender isn't designed for that scenario.

You have a couple of choices here:

Multiple FileAppender with minimal locking

Use multiple FileAppender instances pointing at the same file and configured with minimal locking. This will allow concurrent write operations from multiple processes:

<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="Log.txt" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern
            value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
</appender>

Multiple EventLogAppender

Use multiple EventLogAppender instances that write to a shared Windows event source:

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
    <applicationName value="MyApp" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern
            value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
</appender>

Of course you could also send the log to a database or to a remote component running in a different process or even a different machine but these choices require more infrastructure to be setup so it may be overkill for your scenario.

Related resources:

瞄了个咪的 2024-11-09 04:42:00

当您想要记录到滚动文件并避免写入同一日志文件的风险时,另一个选择是 以编程方式更改 log4net 日志文件的名称 路径。例如,您可以将日志文件名或其一部分作为进程启动参数传递,然后从代码中进行设置。

只需确保在发生任何日志记录之前更改路径即可。否则,可以使用与之前答案中的“具有最小锁定的多个 FileAppender”类似的配置。

Another option when you want to log to the rolling files and avoid risks of writing to the same log file is to programmatically change the name of the log4net log file path. For example, you could pass the log file name or some part of it as process start-up parameter and then set it from the code.

Just make sure you do that path change before any logging occurs. Otherwise, similar configuration to the 'Multiple FileAppender with minimal locking' from the previous answer can be used.

往事风中埋 2024-11-09 04:42:00

通过在日志文件路径中添加任何环境变量来解决此问题的最简单选择。例如:就我而言,我们需要每个 Windows 用户配置文件的应用程序实例。因此,我们在“RollingFileAppender”的日志文件路径中包含“USERNAME”环境变量,如下所示:

 <file value="Logs/${USERNAME}/Log.txt"/>

Easiest option to solve it by adding any environment variable in the path of log file. For example: In my case, we need application instance per windows user profile. So we include "USERNAME" environment variable in log file path of "RollingFileAppender" as below:

 <file value="Logs/${USERNAME}/Log.txt"/>
悲欢浪云 2024-11-09 04:42:00

可以使用 log4net 属性通过 RollingFileAppender 创建唯一的文件名。

在代码中将该属性设置为某个唯一值。选择一个标识进程的值,但不要使用进程 ID。通常会有一些选项或参数可以使用,或者您可以添加一个新选项(例如 --instanceName="MyInstance7"):

log4net.GlobalContext.Properties["InstanceName"] = options.InstanceName;

注意:这必须在配置日志记录(XmlConfigurator)之前完成。

此属性值可以在 RollingFileAppender (app.config) 的配置中使用:
(注意:文件元素的类型属性必须设置为 type="log4net.Util.PatternString"

<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
        <file type="log4net.Util.PatternString" value="${TEMP}\MyProgram_%property{InstanceName}-log.txt"/>

文件名将变为 MyProgram_MyInstance7-log.txt 等。

此解决方案也在 SO 上的其他线程中进行了描述: https://stackoverflow.com/a/572251/1688642< /a>

It is possible to use log4net properties to create unique file names with the RollingFileAppender.

In the code set the property to some unique value. Choose a value that identifies the process, but don't use the process ID. Usually there will be som option or parameter that can be used or you can add a new option (e.g. --instanceName="MyInstance7"):

log4net.GlobalContext.Properties["InstanceName"] = options.InstanceName;

NOTE: This must be done before configuring the logging (XmlConfigurator).

This property value can be used in the configuration for the RollingFileAppender (app.config):
(NOTE: The type attribute for the file-element must be set to type="log4net.Util.PatternString")

<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
        <file type="log4net.Util.PatternString" value="${TEMP}\MyProgram_%property{InstanceName}-log.txt"/>

The filename(s) would then become MyProgram_MyInstance7-log.txt, etc.

This solution is also described in other threads on SO: https://stackoverflow.com/a/572251/1688642

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