我正在编写一个程序,我在其中使用NLog。我有几个课程,在所有课程中我都有属性
public string DeviceName { get; set; }
我希望记录器将它放在我的日志消息前面,所以如果我有两个类
class A
{
public DeviceName {get;set;} = "HMP20"
public void methodOne()
{
logger.Info("Something");
}
}
class B
{
public DeviceName {get;set;} = "HMP30"
public void methodOne()
{
logger.Info("Something");
}
}
然后日志文件中的输出应为
HMP20: Something
HMP30: Something
如何实现这一目标?
答案 0 :(得分:1)
The fast solution is to use the logger-name as device-name:
public class A
{
public string DeviceName {get => logger.Name; set => logger = NLog.LogManager.GetLogger(value); }
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
}
public class B
{
public string DeviceName {get => logger.Name; set => logger = NLog.LogManager.GetLogger(value); }
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
}
Then you can use the following layout-renderer: ${logger:shortname=true}