在运行时几次更改日志文件的名称 [英] Change name of logfile a few times during runtime

查看:85
本文介绍了在运行时几次更改日志文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序运行时,是否可以为fileappender更改日志文件的名称? 每天要进行几次.

Is it possible to change the name of a logfile for a fileappender while the application is running? It will be done a few times / day.

我将尝试进一步详细说明自己:我的应用在设备上写入固件.用户已经使用的所有设备都在网格中.用户可以启动新的书写向导,也可以在已启动的设备上继续执行或重新启动操作. 我想做的是记录用户为特定设备执行的所有步骤的日志.

I'll try to elaborate myself a bit more: my app writes firmware on a device. All the devices the user already worked on, are in a grid. The user can start a new writing-wizard or can resume or restart the action on an already started device. What I would like to do is keep a log of all the steps performed by the user for a certain device.

例如:当用户在设备AB0124上工作时,我要写入名为AB0124.log的日志文件.当他结束在该设备上工作并在XY5618设备上启动时,我想在XY5618.log中记录这些操作

For example: when the user works on device AB0124, I want to write to a logfile called AB0124.log. When he ends working on that device and starts on device XY5618 I want to log those actions in XY5618.log

我已阅读到可以使用上下文属性(此处和一个很多其他帖子),但是您必须在创建记录器之前设置属性. 因此,不是在类中创建记录器,而是在设置属性后在我的方法中创建一个记录器. 但是到目前为止,什么都没有记录.

I've read that it's possible to use a context-property (here and here and a lot of other posts), but you have to set the property before creating the logger. So, instead of creating a logger in the class, I create one in my method after setting the property. But so far, nothing gets logged.

当我在配置中设置硬编码的文件名时,它就起作用了. 我在这里想念东西吗?

When I set the filename hardcoded in the config, it's working. Am I missing somehting here?

Log4Net.config:

<appender name="StepsLogAppender" type="log4net.Appender.FileAppender">
  <filter  type="log4net.Filter.LevelMatchFilter">
    <levelToMatch value="INFO"/>
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
  <file type="log4net.Util.PatternString" value="%property{LogPathModifier}" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{dd/MM/yyyy  -  HH:mm:ss} - %message%newline" />
  </layout>
</appender>

<root>
  <level value="ALL" />
  <appender-ref ref="StepsLogAppender" />
</root>

C#:

public void WriteStepInfo(string device, int step)
{
    log4net.ThreadContext.Properties["LogPathModifier"] = string.Format("D:\\StepsDevice_{0}.txt", device);
    var log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    log.Info(string.Format("Device {0} - step {1}.", device, step));
}

AssemblyInfo.cs中:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

推荐答案

Peter的回答为我带来了正确的方向,但是我最终还是用代码完成了该工作,而不是编辑和保存配置文件.

The answer of Peter brought me in the right direction, but I ended up doing it in code instead of editing and saving the config-file.

public void WriteStepInfo(string device, int step)
{
    var h = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
    foreach (IAppender a in h.Root.Appenders)
    {
        if (a.Name == "StepsLogAppender")
        {
            FileAppender fa = (FileAppender)a;
            var logFileLocation = string.Format(".\\Logs\\Device_{0}.log", device);

            fa.File = logFileLocation;
            fa.ActivateOptions();
            break;
        }
    }

    Log.Info(string.Format("Device {0} - step {1}. Different file for each device", device, step));
}

这篇关于在运行时几次更改日志文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆