【发布时间】:2020-05-19 22:10:28
【问题描述】:
如何在 UWP 项目中配置 NLog 以在 Visual Studio Window:Output 中显示日志?
请注意,在 UWP 应用中 Console.WriteLine("hello") 不起作用。
要在 VisualStudio Window:Output 中编写,您必须使用来自 System.Diagnostic 的 Debug.WriteLine("hello")。
我在下面使用(几乎是默认的)NLog 配置。
登录到文件效果很好。登录到控制台不起作用。
var config = new NLog.Config.LoggingConfiguration();
var storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = storageFolder.Path + @"\file.txt" };
var logconsole = new NLog.Targets.ConsoleTarget("logconsole");
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logconsole);
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
NLog.LogManager.Configuration = config;
【问题讨论】:
-
是的,UWP 不喜欢控制台。但是你可以试试
OutputDebugString。见github.com/nlog/nlog/wiki/OutputDebugString-target