Log4Net 和多个日志文件

发布于 2024-12-11 05:19:31 字数 792 浏览 0 评论 0原文

互联网上有几个关于同一主题的帖子,我还可以从我的 Windows 窗体应用程序写入多个日志文件。但我的要求略有不同。

我的应用程序有两种运行模式,例如“BuySomething”模式和“BuySomething”模式。 “出售其他东西”模式。当它处于“BuySomething”模式时,我想写入 Log_BuySomething.txt 和 Log_SellSomeOtherThing.txt (肯定会选择一种模式)。

在 app.config 文件中,我的结构与 StackOverflow 问题
我的问题是,当执行代码 XmlConfigurator.Configure(); 时,它会创建 app.config 文件中两个 LogFileAppender 中提到的名称的空日志文件。我认为以下代码可以解决这个问题,但事实并非如此:

if (mode == BuySomeThing)
{
    logger = LogManager.GetLogger("LogFileAppender1");
}
else
{
    LogManager.GetLogger("LogFileAppender2");
}
XmlConfigurator.Configure();

如何确保只为应用程序的该实例创建适当的日志文件?

There are several posts on the internet for the same topic and I am also able to write multiple log files from my Windows Forms App. But my requirement is slightly different.

My app has two modes of running, say like "BuySomething" mode & "SellSomeOtherThing" mode. And when it is in "BuySomething" mode I want to write to Log_BuySomething.txt and to Log_SellSomeOtherThing.txt otherwise (one mode will be selected for sure).

In the app.config file, I have the same structure as in the last post of a StackOverflow Question.
My problem is that, when the code XmlConfigurator.Configure(); is executed, it creates empty log files of the names mentioned in both LogFileAppenders in the app.config file. I thought the following code would solve it, but it didn't:

if (mode == BuySomeThing)
{
    logger = LogManager.GetLogger("LogFileAppender1");
}
else
{
    LogManager.GetLogger("LogFileAppender2");
}
XmlConfigurator.Configure();

How can I make sure that only the appropriate log file is created for that instance of the App?

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

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

发布评论

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

评论(1

倾听心声的旋律 2024-12-18 05:19:31

如果我理解你的意思,你的应用程序不会在模式之间切换。

如果是这样,那么我怀疑使用您的代码会创建这两个文件,但实际上只写入一个文件。

如果摆脱零字节文件很重要,您可以尝试以下操作:

log4net.GlobalContext.Properties["MyLogFileName"] = 
   (mode == BuySomeThing) ? "Log_BuySomething.txt" : "Log_SellSomeOtherThing.txt" ;

XmlConfigurator.Configure();

您还需要将配置文件更改为只有一个附加程序,并告诉它使用此属性作为文件名,例如

<appender name="FileAppender" type="log4net.Appender.FileAppender">
   <file type="log4net.Util.PatternString" value="Logfiles\%property{MyLogFileName}" />

    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

这应该最终只创建一个文件。 (在此示例中,它将在文件夹 logfiles 中创建)。

If I understand you, your app doesn't switch between modes.

If so, then I would suspect that using your code will create both files, but only actually write to one.

If getting rid of the zero-byte file is important you could try something like:

log4net.GlobalContext.Properties["MyLogFileName"] = 
   (mode == BuySomeThing) ? "Log_BuySomething.txt" : "Log_SellSomeOtherThing.txt" ;

XmlConfigurator.Configure();

You also need to change your config file to only have one appender and tell it to use this property for the file name, e.g.

<appender name="FileAppender" type="log4net.Appender.FileAppender">
   <file type="log4net.Util.PatternString" value="Logfiles\%property{MyLogFileName}" />

    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

This should end up creating just one file. (In this example it will be created in the folder logfiles).

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