日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

06Prism WPF 入门实战 - Log控件库

發布時間:2023/12/4 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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" ?> <nlog 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"> ? //日志的錯誤位置文件<variable name="logDirectory" value="${basedir}/logs"/><!-- the targets to write to --><targets async="true"><!-- write logs to file --><target 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}" /><!-- another file log, only own logs. Uses some ASP.NET core renderers --><target 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}" /><!--|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}--><!-- write log message to database --><!--<target name="db" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">--><target type="Database" name="db" dbProvider="Npgsql.NpgsqlConnection,Npgsql" ? ?《這里的數據庫名字注意查找》connectionString="Database=backofficev2;Host=*;User Id=*;Password=*;pooling=false;port=*;"><commandText> //使用postgresql 這里的字段要加雙引號,timestamp要將string類型的轉換為timestamp類型INSERT INTO "SystemLog"("Source","Level","Content","CreatedAt") VALUES(@source, @level, @content, TO_TIMESTAMP(@createdAt, 'YYYY-MM-DD HH24:MI:SS'));</commandText> <!-- database connection parameters ${logger} Server--> ? <-數據庫中要寫的字段-><parameter name="@source" layout="Server" /><parameter name="@level" layout="${level}" /><parameter name="@content" layout="${message}" /><parameter name="@createdAt" layout="${date}" /></target><!--</target>--></targets><!-- rules to map from logger name to target --><rules><!--TRACE,DEBUG,INFO,WARN,ERROR,FATAL警告級別控制--> ?<logger name="*" minlevel="Trace" writeTo="allfile" /><!--INFO,WARN,ERROR,FATAL--><logger name="AiEcgWebApi.Controllers.*" minlevel="Warn" writeTo="db" /><!--DEBUG,INFO,WARN,ERROR,FATAL--><logger name="*" minlevel="Debug" writeTo="ownFile-web" /> ? </rules> </nlog>

(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 全局異常捕捉

出錯的任務中未觀察到的異常將觸發異常呈報策略時出現。

/// <summary> /// 應用程序啟動時創建Shell /// </summary> /// <returns></returns> 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安裝:MaterialDesignInXamlToolkit2.選擇主題 Light theme:<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" /> Dark theme: ? <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />3.App文件: <Application x:Class="MaterialTest.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"StartupUri="MainWindow.xaml"><Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" /><ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" /><ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" /><ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" /></ResourceDictionary.MergedDictionaries> ? ? ? ? ? ?</ResourceDictionary></Application.Resources> </Application>4.配置View <Window [...]TextElement.Foreground="{DynamicResource MaterialDesignBody}"Background="{DynamicResource MaterialDesignPaper}"TextElement.FontWeight="Medium"TextElement.FontSize="14"FontFamily="pack://application:,,,/MaterialDesignThemes.Wpf;component/Resources/Roboto/#Roboto"[...] >

總結

以上是生活随笔為你收集整理的06Prism WPF 入门实战 - Log控件库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。