Windows 10 个通用应用中的事件记录器
Event logger in Windows 10 Universal Apps
我正在尝试为 Windows 通用应用程序创建事件日志。
早些时候我们有 System.Diagnostics EventLog
来记录事件,但我在 Windows 10 Universal Apps 平台上找不到类似的东西。
是否可以为 Windows 10 创建日志,是否可以将这些日志写入文件供以后访问?
我搜索了很多,但找不到任何东西。
FileLoggingSession
由于Windows 8.1
在Windows.Foundation.Diagnostics
命名空间中有FileLoggingSession
和LoggingChannel
classes,当配置为这样做时,它们可以执行记录到文件.您可以在官方阅读更多内容 documentation.
初始化、使用和检索日志文件可以像下面的代码片段一样完成,当然您需要创建接口、单例等以使其可用:
// Initialization
FileLoggingSession fileLoggingSession = new FileLoggingSession("session");
var loggingChannel = new LoggingChannel("channel");
fileLoggingSession.AddLoggingChannel(loggingChannel);
// Log messages
loggingChannel.LogMessage("error message", LoggingLevel.Error);
// When file is needed
var file = await fileLoggingSession.CloseAndSaveToFileAsync();
// Do anything with file
LoggingSession
就像FileLoggingSession
将日志写入文件,但主要区别在于FileLoggingSession
会立即将日志写入文件,而LoggingSession
则不会,需要您手动请求使用 SaveToFileAsync
方法将日志写入文件。来自文档:
The FileLoggingSession class sends logged messages to disk files as they are logged. The FileLoggingSession class uses sequential logging, which means that all messages are sent to a disk file, and a sequential history of messages is retained. This is distinct from the LoggingSession class, which sends logged messages to disk on-demand, and this happens when there's a problem and the immediate history of in-memory messages is needed for analysis.
MetroLog
如果您不想使用 FileLoggingSession
或 LoggingSession
class,您还有其他选择。一个好的解决方案是 MetroLog,它有一个 FileStreamingTarget
目标,这使得登录 Windows/Phone 应用程序变得非常简单。
您在需要时创建记录器,例如在页面中:
public sealed partial class LogSamplePage : Win8Sample.Common.LayoutAwarePage
{
private ILogger Log = LogManagerFactory.DefaultLogManager.GetLogger<LogSamplePage>();
}
那么在页面中就可以这样使用了:
// flat strings...
if (this.Log.IsInfoEnabled)
this.Log.Info("I've been navigated to.");
// formatting...
if (this.Log.IsDebugEnabled)
this.Log.Debug("I can also format {0}.", "strings");
// errors...
try
{
this.DoMagic();
}
catch(Exception ex)
{
if (this.Log.IsWarnEnabled)
this.Log.Warn("You can also pass in exceptions.", ex);
}
MetroEventSource
第二个解决方案是 this Can Bilgin 在 MSDN 示例库中记录示例,其中有 MetroEventSource
class。您可以记录消息,例如这样的错误:
MetroEventSource.Log.Error("Here is the error message");
如果您使用此记录器,请不要忘记在应用程序 运行 上对其进行初始化,如示例项目中所述。
我正在尝试为 Windows 通用应用程序创建事件日志。
早些时候我们有 System.Diagnostics EventLog
来记录事件,但我在 Windows 10 Universal Apps 平台上找不到类似的东西。
是否可以为 Windows 10 创建日志,是否可以将这些日志写入文件供以后访问?
我搜索了很多,但找不到任何东西。
FileLoggingSession
由于Windows 8.1
在Windows.Foundation.Diagnostics
命名空间中有FileLoggingSession
和LoggingChannel
classes,当配置为这样做时,它们可以执行记录到文件.您可以在官方阅读更多内容 documentation.
初始化、使用和检索日志文件可以像下面的代码片段一样完成,当然您需要创建接口、单例等以使其可用:
// Initialization
FileLoggingSession fileLoggingSession = new FileLoggingSession("session");
var loggingChannel = new LoggingChannel("channel");
fileLoggingSession.AddLoggingChannel(loggingChannel);
// Log messages
loggingChannel.LogMessage("error message", LoggingLevel.Error);
// When file is needed
var file = await fileLoggingSession.CloseAndSaveToFileAsync();
// Do anything with file
LoggingSession
就像FileLoggingSession
将日志写入文件,但主要区别在于FileLoggingSession
会立即将日志写入文件,而LoggingSession
则不会,需要您手动请求使用 SaveToFileAsync
方法将日志写入文件。来自文档:
The FileLoggingSession class sends logged messages to disk files as they are logged. The FileLoggingSession class uses sequential logging, which means that all messages are sent to a disk file, and a sequential history of messages is retained. This is distinct from the LoggingSession class, which sends logged messages to disk on-demand, and this happens when there's a problem and the immediate history of in-memory messages is needed for analysis.
MetroLog
如果您不想使用 FileLoggingSession
或 LoggingSession
class,您还有其他选择。一个好的解决方案是 MetroLog,它有一个 FileStreamingTarget
目标,这使得登录 Windows/Phone 应用程序变得非常简单。
您在需要时创建记录器,例如在页面中:
public sealed partial class LogSamplePage : Win8Sample.Common.LayoutAwarePage
{
private ILogger Log = LogManagerFactory.DefaultLogManager.GetLogger<LogSamplePage>();
}
那么在页面中就可以这样使用了:
// flat strings...
if (this.Log.IsInfoEnabled)
this.Log.Info("I've been navigated to.");
// formatting...
if (this.Log.IsDebugEnabled)
this.Log.Debug("I can also format {0}.", "strings");
// errors...
try
{
this.DoMagic();
}
catch(Exception ex)
{
if (this.Log.IsWarnEnabled)
this.Log.Warn("You can also pass in exceptions.", ex);
}
MetroEventSource
第二个解决方案是 this Can Bilgin 在 MSDN 示例库中记录示例,其中有 MetroEventSource
class。您可以记录消息,例如这样的错误:
MetroEventSource.Log.Error("Here is the error message");
如果您使用此记录器,请不要忘记在应用程序 运行 上对其进行初始化,如示例项目中所述。