C# 关于使用NLog的几个问题
我安装了Nlog并进行了如下配置C# 关于使用NLog的几个问题,c#,nlog,C#,Nlog,我安装了Nlog并进行了如下配置 public static Logger _logger = null; static void foo() { var config = new LoggingConfiguration(); var fileTarget = new FileTarget(); config.AddTarget("file", fileTarget); // Step 3. Set
public static Logger _logger = null;
static void foo()
{
var config = new LoggingConfiguration();
var fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
// Step 3. Set target properties
fileTarget.Layout = @"${date:format=HH\:mm\:ss} - ${message}";
fileTarget.FileName = "c:/myFolder/" + "${date:format=yyyy-MM-dd}.log";
var rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);
config.LoggingRules.Add(rule2);
// Step 5. Activate the configuration
LogManager.Configuration = config;
// Example usage
_logger = LogManager.GetLogger("Example");
_logger.Error("error log message");
}
但我想问一些问题
foo
,它使用NLog写入日志文件。现在,当有人想要使用我的DLL时,他们可以指定NLog应该写入哪个文件夹。现在,当其他程序开始使用我的DLL时,我希望它们也能够指定要写入的其他文件夹。你看到了吗?因此,程序A使用我的DLL写入文件夹C,但程序B同时也使用我的DLL写入文件夹DconcurrentWrites
属性来控制它LogManager.GetLogger()
方法时,Nlog尽可能返回相同的记录器实例FileName
fileTarget.FileName=“c:/myFolder/${processid}/${date:format=yyyy-MM-dd}.log”
或
fileTarget.FileName=“c:/myFolder/${processname}/${date:format=yyyy-MM-dd}.log”
fileTarget.FileName
// imagine this method returns "c:/myPath"
string logFolderPath = AskPathFromUser();
// since your logPath.txt file is app relative.
// each process will have own file next to process file.
File.WriteAllText("logPath.txt", logFolderPath);
// now every time you need to set the path read the saved path from file
fileTarget.FileName=Path.Combine(File.ReadAllText("logPath.txt"),"${date:format=yyyy-MM-dd}.log");
第一个问题是
fileTarget.FileName=“c:/myFolder/${date:format=yyyy-MM-dd}.log”
。你能解释一下你的2en问题吗?我已经更新了我的问题,如果你能根据我更新的问题调整你的答案,那就太好了。另外,用户通常指定的路径不像c:/myFolder,而是像c:\\somefolder\\otherfolder等-不同的斜杠。如何处理?请参阅我的最新帖子。您可以使用string.Replace()
方法。我想我需要一个单独的问题,但我的意思是。我说我的DLL正在使用NLog。假设我的DLL有使用NLog写入日志文件的方法foo。现在,当有人想要使用我的DLL时,他们可以指定NLog应该写入哪个文件夹。现在,当其他程序开始使用我的DLL时,我希望它们也能够指定要写入的其他文件夹。你看到了吗?因此,程序A写入文件夹C:使用我的DLL,但程序B写入文件夹D:同时也使用我的DLL。好的,这不是我的意思,但我会接受它,因为我希望一个用户写入一个日志,另一个用户写入另一个日志