06Prism WPF 入门实战 - Log&控件库
1.概要
源码及PPT地址:https://github.com/JusterZhu/wemail
视频地址:https://www.bilibili.com/video/BV1KQ4y1C7tg?share\source=copy\web
本章分为以下几个部分来了解:
Part1 日志
Part1.1 全局异常捕捉
Part1.2 Dump
Part2 引入控件库
2.详细内容
Part1 日志
(1)Nuget安装:
Microsoft.Extensions.Logging.Abstractions
NLog.Extensions.Logging
NLog.Config
(2)配置Nlog.config
<?xml version="1.0" encoding="utf-8" ?>
xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="info"
throwException ="true"
internalLogFile="logs/internal-nlog.txt"> //日志的错误位置文件
name="logDirectory" value="${basedir}/logs"/>
async="true">
xsi:type="File" name="allfile" fileName="${logDirectory}/nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
xsi:type="File" name="ownFile-web" fileName="${logDirectory}/nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
type="Database" name="db" dbProvider="Npgsql.NpgsqlConnection,Npgsql" 《这里的数据库名字注意查找》connectionString="Database=backofficev2;Host=*;User Id=*;Password=*;pooling=false;port=*;">
//使用postgresql 这里的字段要加双引号,timestamp要将string类型的转换为timestamp类型
INSERT INTO "SystemLog"("Source","Level","Content","CreatedAt") VALUES(@source, @level, @content, TO_TIMESTAMP(@createdAt, 'YYYY-MM-DD HH24:MI:SS'));
<-数据库中要写的字段->
name="@source" layout="Server" />
name="@level" layout="${level}" />
name="@content" layout="${message}" />
name="@createdAt" layout="${date}" />
name="*" minlevel="Trace" writeTo="allfile" />
name="AiEcgWebApi.Controllers.*" minlevel="Warn" writeTo="db" />
name="*" minlevel="Debug" writeTo="ownFile-web" />
(3)App.cs注入log组件
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
var factory = new NLog.Extensions.Logging.NLogLoggerFactory();
Microsoft.Extensions.Logging.ILogger logger = factory.CreateLogger("NLog");
containerRegistry.RegisterInstance(logger);
}
(4)ViewModel构造函数获取log引用
public MainWindowViewModel(ILogger logger)
{
logger.LogInformation("hhhhhhh");
}
Part1.1 全局异常捕捉
出错的任务中未观察到的异常将触发异常呈报策略时出现。
///
/// 应用程序启动时创建Shell
///
///
protected override Window CreateShell()
{
//UI线程未捕获异常处理事件
this.DispatcherUnhandledException += OnDispatcherUnhandledException;
//Task线程内未捕获异常处理事件
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
//多线程异常
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
return Container.Resolve<MainWindow>();
}
Part1.2 Dump
程序异常崩溃前使用此类为进程创建DUMP文件,之后可以使用WinDbg等工具进行分析。(该文件包含一些敏感信息切勿将公司项目中的dump文件公布到互联网上)
Windebug分析案例:
https://mp.weixin.qq.com/s/i6cJHTrIPweDIplzzfHnVQ
Windebug分析教程:
https://docs.microsoft.com/zh-cn/windows-hardware/drivers/debugger/getting-started-with-windows-debugging?WT.mc_id=WDIT-MVP-5004326
Windebug命令:
https://docs.microsoft.com/zh-cn/windows-hardware/drivers/debugger/commands?WT.mc_id=WDIT-MVP-5004326
Part2 控件库
1.Nuget安装:MaterialDesignInXamlToolkit
2.选择主题
Light theme:
Dark theme:
3.App文件:
4.配置View