Logging in .NET Core Windows Forms or WPF Application

Logging in NET Core Windows Forms

In this article, we will learn, how to add Logging in .NET Core Windows Forms or WPF Application.

As we know .NET Core has introduced ILogger as a generic interface for logging purposes.

This framework supported interface ILogger can be used across different types of applications like,

  • Desktop or Form applications (.NET Core 3.0 and above )

Unlike .NET Core in WebAPI, the Console app or Forms App doesn’t have dependency injection by default. In ASP.NET WebAPI it was easy configuring ILogger through DI.

Today in this article, we will cover below aspects,

ILogger interface works very nicely with the .NET Core ecosystem and today in this post we will learn how to enable logging in a .NET Forms or Desktop application.

Getting Started -Logging in .NET Core Windows Forms

Here I am using a Forms/Windows .NET Core 3.0 application.

Logging in NET Core Windows Forms

The below code illustrates how to achieve logging in Desktop applications.

There could be multiple ways of achieving or an even better approach might be available.

Add ILogger using Dependency Injection to Forms App

Below is the example of what we can achieve ultimately for the logger and implement Logging in .NET Core Windows Forms

In the above code, we added a logging ILogger interface to services collection and built the ServiceProvider for the required services.

With that ILogger instance can be DI via Constructor injection as shown above,

The complete sample code is as below,

public partial class Form1 : Form
    {
        private readonly ILogger _logger;
        public Form1(ILogger<Form1> logger)
        {
            _logger = logger;
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                _logger.LogInformation("Form1 {BusinessLayerEvent} at {dateTime}", "Started", DateTime.UtcNow);
                // Perform Business Logic here 
               
                MessageBox.Show("Hello .NET Core 3.1.This is First Forms app in .NET Core");
                _logger.LogInformation("Form1 {BusinessLayerEvent} at {dateTime}", "Ended", DateTime.UtcNow);
            }
            catch (Exception ex)
            {
                //Log technical exception 
                _logger.LogError(ex.Message);
            }
        }

To achieve the above here is below how I updated the Main() method for Service collection for required interfaces.

Application class in Forms application expect static method innovation.

Due to the same reason, I made use of the same class to provide a start-up for First Forms to be injected and then injected all required services through that Forms instance.

        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var services = new ServiceCollection();
            ConfigureServices(services);
            using (ServiceProvider serviceProvider = services.BuildServiceProvider())
            {
                var form1 = serviceProvider.GetRequiredService<Form1>();
                Application.Run(form1);
            }
        }

ConfigureService() method is added as below to take care of all services required in the App. Add all your DI services to the service collection.

Below I am creating a singleton instance of the Form object and Scoped lifetime instance of the other business objects.

private static void ConfigureServices(ServiceCollection services)
        {
            services.AddSingleton<Form1>()
                    .AddLogging(configure => configure.AddConsole())
                    .AddScoped<IBusinessLayer, CBusinessLayer>()
                    .AddScoped<IDataAccessLayer, CDataAccessLayer>();
        }

We just enabled Console logging using the below code in the above example,

services.AddLogging(configure => configure.AddConsole())

Please add below NuGet packages explicitly to your application.

  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Logging
  • Microsoft.Extensions.Logging.Console

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" />
  </ItemGroup>

Here above using statements internally call dispose and flush out console logs.

I configured the Console Logging by changing the application output type as Console app so that Console logs on the Command prompt can be visible.

Let’s run the application and check to log,

blank

Updated Forms class for DI of ILogger and IBusinessLayer object is as below,

blank

Dependency Injection Windows Form using Generic Host Builder

Additionally please use Generic Host builder for performing DI for Logging and other business requirements.

Reference:

Can we make the above code better? Please let me know if you have any better suggestions. Please sound off your comments below.

Summary

Today we looked at simple and easy-to-use techniques to enable logging in to a .NET Core Forms application. Unlike .NET Core in WebAPI, the .NET Core Forms app doesn’t have dependency injection, but with small custom changes as mentioned above, we can very much achieve similar functionality.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.