[英]NLOG: Creating multiple instances of same class writing log to same file
I am trying to get used to NLOG and finding it difficult in setting log files for multiple instances of a same class.我正在尝试习惯 NLOG 并发现为同一类的多个实例设置日志文件很困难。 Each class is created on a different thread.每个类都是在不同的线程上创建的。 Please find below the sample code:请在下面找到示例代码:
{
ILogger mLogger;
public string Name { get; set; }
public string SecondName { get; set; }
private string mFilePath = @"C:\Logs\";
public test(string name, string secondname)
{
var target = new FileTarget(name) { FileName = mFilePath + name + ".txt" };
var config = new LoggingConfiguration();
config.AddRule(LogLevel.Info, LogLevel.Info, target);
LogManager.Configuration = config;
mLogger = LogManager.GetLogger(name);
LogManager.ReconfigExistingLoggers();
mLogger.Info("Hi from :" + name);
}
}
static void Main(string[] args)
{
new Thread(new ThreadStart(() => { new test("Stuart", "Broad"); })).Start();
new Thread(new ThreadStart(() =>
{
new test("John", "Smith");
})).Start();
new Thread(new ThreadStart(() =>
{
new test("Virat", "Kohli");
})).Start();
new Thread(new ThreadStart(() =>
{
new test("Christiano", "Ronaldo");
})).Start();
new Thread(new ThreadStart(() =>
{
new test("Pat", "cummins");
})).Start();
Console.Read();
}
and output is : 2020-03-03 11:36:56.9404|INFO|Christiano|Hi from :Christiano 2020-03-03 11:36:56.9404|INFO|Stuart|Hi from :Stuart 2020-03-03 11:36:56.9404|INFO|John|Hi from :John 2020-03-03 11:36:56.9404|INFO|Virat|Hi from :Virat 2020-03-03 11:36:56.9404|INFO|Pat|Hi from :Pat输出为: 2020-03-03 11:36:56.9404|INFO|Christiano|Hi from :Christiano 2020-03-03 11:36:56.9404|INFO|Stuart|Hi from :Stuart 2020-03-063 11 :56.9404|INFO|John|嗨,来自:John 2020-03-03 11:36:56.9404|INFO|Virat|嗨,来自:Virat 2020-03-03 11:36:56.9404|INFO|Pat|嗨,来自:Pat
from just one file Christiano.txt仅来自一个文件 Christiano.txt
Could somebody please help me in configuring this?有人可以帮我配置这个吗?
Thank you.谢谢你。
LogManager.Configuration
is a static variable. LogManager.Configuration
是一个静态变量。 When you assign it then you affect everyone else.当您分配它时,您就会影响其他所有人。
Instead give a unique Logger for each thread.而是为每个线程提供一个唯一的 Logger。 The same FileTarget can handle multiple Loggers.同一个 FileTarget 可以处理多个 Logger。
{
ILogger mLogger;
public string Name { get; set; }
public string SecondName { get; set; }
public test(string name, string secondname)
{
mLogger = LogManager.GetLogger(name);
mLogger.Info("Hi from :" + name);
}
}
static void Main(string[] args)
{
string mFilePath = @"C:\Logs\";
var target = new FileTarget(name) { FileName = Path.Combine(mFilePath, "${logger}.txt") };
var config = new LoggingConfiguration();
config.AddRule(LogLevel.Info, LogLevel.Info, target);
LogManager.Configuration = config;
new Thread(new ThreadStart(() => { new test("Stuart", "Broad"); })).Start();
new Thread(new ThreadStart(() =>
{
new test("John", "Smith");
})).Start();
new Thread(new ThreadStart(() =>
{
new test("Virat", "Kohli");
})).Start();
new Thread(new ThreadStart(() =>
{
new test("Christiano", "Ronaldo");
})).Start();
new Thread(new ThreadStart(() =>
{
new test("Pat", "cummins");
})).Start();
Console.Read();
}
Alternative then you could assign names to the threads , and make use of the NLog ${threadname}
instead of ${logger}
in FileName-Layout.或者,您可以为线程分配名称,并在 FileName-Layout 中使用 NLog ${threadname}
而不是${logger}
。
Yet another alternative is to create a scope using NLog MappedDiagnosticsLogicalContext.SetScoped("TestName", name) and use ${mdlc:TestName}
in FileName-Layout.另一种选择是使用 NLog MappedDiagnosticsLogicalContext.SetScoped("TestName", name)创建一个范围,并在 FileName-Layout 中使用${mdlc:TestName}
。 This will also allow you to flow the initial context to async Tasks.这也将允许您将初始上下文流到异步任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.