Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to register Nlog log service for WPF framework Prism

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to register for the WPF framework Prism Nlog log service", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to register for the WPF framework Prism Nlog log service" this article.

Both Nlog and Serilog provide a quick way to use it quickly in a variety of applications.

Although, you can now use ServiceCollection to do this in both WPF and ASP.NET Core, because the logging framework provides extensions to IServiceCollection.

However, if you are using Prism 8.0 application, Prism provides support for a variety of containers, such as DryIoc or Unity. At this time, if we implement dependency injection on this basis, we first need to modify the default implementation of creating containers in Prism, where ServiceCollection is appended to the container.

The example in this article mainly takes the DryIoc container as an example:

Several related dependencies will be used here:

Microsoft.Extensions.DependencyInjection

Microsoft.Extensions.Logging

DryIoc.Microsoft.DependencyInjection

NLog.Extensions.Logging

To do this, you need to add some related packages, as follows:

Nlog.Config: main configuration Nlog execution configuration, rules

NLog.Extensions.Logging: extension method for registering services

In the App.xaml.cs code, it looks like this:

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)) }

Window, add the test code:

Public partial class MainWindow: Window {private readonly Logger logger; public MainWindow (Logger logger) {InitializeComponent (); this.logger = logger;} private void Button_Click (object sender, RoutedEventArgs e) {logger.LogDebug ("Hello");}}

Note: to configure Nlog, you need to modify the Nlog.Config configuration file to take effect. Please refer to the Github documentation. The following is the test configuration:

The final output is as follows:

2021-08-19 16 Hello 32 Hello 00.5558 | 0 | DEBUG | wpflogapp.MainWindow | DEBUG | wpflogapp.MainWindow | Hello 2021-08-19 16V 32 Hello 00.8828 | 0 | DEBUG | wpflogapp.MainWindow | Hello 2021-08-19 16 Swiss 32v 320.1.0647 | 0 | DEBUG | wpflogapp.MainWindow | Hello 2021-08-19 1622 32 Swiss 01.2608 | 0 | DEBUG | wpflogapp.MainWindow | Hello

The code of the complete App.xaml.cs file is as follows:

Public partial class App: PrismApplication {protected override Window CreateShell () {return Container.Resolve ();} 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));}} these are all the contents of the article "how to register Nlog Log Service for WPF Framework Prism". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report