欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

為WPF框架Prism注冊Nlog日志服務(wù)

 更新時(shí)間:2022年02月15日 08:58:29   作者:痕跡g  
這篇文章介紹了為WPF框架Prism注冊Nlog日志服務(wù)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

無論是Nlog還是Serilog, 它們都提供了如何快速在各類應(yīng)用程序當(dāng)中的快速使用方法。

盡管,你現(xiàn)在無論是在WPF或者ASP.NET Core當(dāng)中, 都可以使用ServiceCollection來做到著一點(diǎn), 因?yàn)槿罩究蚣芏继峁┝薎ServiceCollection的擴(kuò)展。

但是, 如果現(xiàn)在你使用的是Prism 8.0的應(yīng)用程序, Prism提供了多種容器的支持, 例如:DryIoc或者Unity, 這個(gè)時(shí)候我們?nèi)绻F(xiàn)在這個(gè)基礎(chǔ)上實(shí)現(xiàn)依賴注入,首先我們需要修改Prism當(dāng)中創(chuàng)建容器的默認(rèn)實(shí)現(xiàn), 在其中將ServiceCollection追加到容器當(dāng)中。

本文的示例主要以DryIoc容器為示例:

這里會(huì)主要用到幾個(gè)相關(guān)的依賴:

  • Microsoft.Extensions.DependencyInjection;
  • Microsoft.Extensions.Logging;
  • DryIoc.Microsoft.DependencyInjection;
  • NLog.Extensions.Logging;

為此, 需要添加一些相關(guān)的包,如下所示:

Nlog.Config: 主要配置Nlog的執(zhí)行配置,規(guī)則

NLog.Extensions.Logging: 擴(kuò)展方法, 用于注冊服務(wù)

在App.xaml.cs代碼,如下所示:

        protected override IContainerExtension CreateContainerExtension()
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddLogging(configure =>
            {
                configure.ClearProviders();
                configure.SetMinimumLevel(LogLevel.Trace);
                configure.AddNLog();
            });

            return new DryIocContainerExtension(new Container(CreateContainerRules())
                .WithDependencyInjectionAdapter(serviceCollection));
        }

窗口中,添加測試代碼:

    public partial class MainWindow : Window
    {
        private readonly Logger<MainWindow> logger;

        public MainWindow(Logger<MainWindow> logger)
        {
            InitializeComponent();
            this.logger = logger;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            logger.LogDebug("Hello");
        }
    }

注意: 配置Nlog需要修改Nlog.Config配置文件生效,可參考Github文檔, 下面為測試配置:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
  
  <targets>
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"  />
  </targets>

  <rules> 
    <logger name="*" minlevel="Debug" writeTo="f" /> 
  </rules>
</nlog>

最終輸出內(nèi)容,如下所示:

2021-08-19 16:32:00.5558|0|DEBUG|wpflogapp.MainWindow|Hello 
2021-08-19 16:32:00.7049|0|DEBUG|wpflogapp.MainWindow|Hello 
2021-08-19 16:32:00.8828|0|DEBUG|wpflogapp.MainWindow|Hello 
2021-08-19 16:32:01.0647|0|DEBUG|wpflogapp.MainWindow|Hello 
2021-08-19 16:32:01.2608|0|DEBUG|wpflogapp.MainWindow|Hello 

完整App.xaml.cs文件代碼如下:

    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        { 
            return Container.Resolve<MainWindow>();
        } 
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        { } 
        protected override IContainerExtension CreateContainerExtension()
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddLogging(configure =>
            {
                configure.ClearProviders();
                configure.SetMinimumLevel(LogLevel.Trace);
                configure.AddNLog();
            });

            return new DryIocContainerExtension(new Container(CreateContainerRules())
                .WithDependencyInjectionAdapter(serviceCollection));
        }
    }

到此這篇關(guān)于為WPF框架Prism注冊Nlog日志服務(wù)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論