/FileLogger.Logging

File Logger Provider

Primary LanguageC#

FileLogger.Logging

This is a submodule to provide a File Logging Provider to be used with .NET Microsoft.Extensions.Logging.

Because the term of "Tripous" used in reference who wasn't not my cup of thea then to enhence little things but it should be a fork.

Samples

Usage

Get github submodule into your project

Execute in command line to your project's root:

git submodule add https://github.com/mabyre/FileLogger.Logging.git

At the start of your project like in Program.cs

    var host = Host.CreateDefaultBuilder()
        .ConfigureLogging(logging =>
        {
            //logging.ClearProviders();
            logging.AddFileLogger();
            //logging.AddFileLogger(options => {
            //    options.MaxFileSizeInMB = 5;
            //});
        })
        .Build();

Instanciate ILogger and ILoggerFactory

    public partial class MainWindow : Window
    {
        private readonly ILogger<MainWindow> _logger;
        private readonly ILoggerFactory _loggerFactory;

        public MainWindow(ILoggerFactory loggerFactory)
        {
            InitializeComponent();

In your program use ILogger

    public partial class YourUserControl : UserControl
    {
        private ILogger<YourUserControl> logger;

        public YourUserControl(ILoggerFactory loggerFactory)
        {
            InitializeComponent();
            
            // In xaml there is some text in Text so add a NexLine
            textBoxLog.Text += Environment.NewLine;

            logger = loggerFactory.CreateLogger<YourUserControl>();
            logger.LogWarning("My UserControl getting initialized");
        }

In appsettings.json file

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Trace",
      "Microsoft.Hosting.Lifetime": "Trace"
    },
    "File": {
      "LogLevel": "Trace",
      "Folder": ".\\Logs"
    },

Samples

Project using this module in a WPF Application:

https://github.com/mabyre/WpfAppCore1

This module used in Blazor Server Application

https://github.com/mabyre/MyBlazorServerApp

Reference