LogViewer是通过UDP传输的高性能实时log查看器。
具有一下特性:
项目地址:https://github.com/Styort/LogViewer
wpf项目采用Prism框架,项目名称:LogToLogViewerApp
nlog库
<PackageReference Include="NLog" Version="5.3.4" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.13" />
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwExceptions="true"
internalLogFile="c:\temp\console-example-internal.log"
internalLogLevel="Info">
<targets async="true">
<target name="log4view" xsi:type="NLogVIewer" address="udp://127.0.0.1:7071"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="log4view"></logger>
</rules>
</nlog>
将nlog.config文件设置成如果较新则复制。
需要的其他依赖包:
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2" />
<PackageReference Include="DryIoc.Microsoft.DependencyInjection" Version="8.0.0-preview-02" />
重写CreateContainerExtension方法如下:
protected override IContainerExtension CreateContainerExtension()
{
var services = new ServiceCollection();
services.AddLogging(builder =>
{
builder.ClearProviders();
builder.SetMinimumLevel(LogLevel.Debug);
builder.AddNLog();
});
var container = new DryIoc.Container(CreateContainerRules());
var x = container.WithDependencyInjectionAdapter(services);
return new DryIocContainerExtension(x.Container);
//return base.CreateContainerExtension();
}
2.4 在mainwindowviewmodel中使用注入ilogger接口并输出日志
在构造函数中注入ilogger,添加一个Log方法用于日志的输出
public MainWindowViewModel(ILogger<MainWindowViewModel> logger)
{
_logger = logger;
}
[RelayCommand]
private void Log()
{
_logger.LogTrace("Log Trace");
_logger.LogDebug("Log Debug");
_logger.LogInformation("Log Information");
_logger.LogWarning("Log Warning");
_logger.LogError("Log Error");
_logger.LogCritical("Log Critical");
}
使用LogViewer可以接收多个app端的日志输出,并可以根据需要选择显示的日志级别。有助于开发人员可以实时关注程序的运行流程。