Monday, 4 March 2019

.net core console app using SeriLog to log to file with config in json file



1. add the packages

* don't need serilog.sinks.console if just logging to file / or not using serilog console (can use standard console logger Microsoft.Extensions.Logging.Console)

2. when the app starts call a setup method to configure everything (need something similar when/if setting up dependancy injection)


2a. in the configuration method, create a config (IConfigurationRoot) based on the json file

2b. pass the config into the configuration of the serilog


2c. add the serilog to the services


* you only need the AddSerilog line. The console lines adds console logging, the addconfiguration line applies configuration for the console logging but doesn NOT effect serilog

3. add a json file to the project

3a. ensure copy local is set so file goes with exe to output directory
Properties -> Copy to Output Directory -> Copy if newer

3b. configure serilog


Entire Program class


    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, starting processing");

            var serviceProvider = ConfigureServices(new ServiceCollection());
                                  
            var core = serviceProvider.GetService<Core>();
            core.Process();

            Console.WriteLine("Done, press enter to exit");
            Console.Read();
        }

        private static IServiceProvider ConfigureServices(IServiceCollection services)
        {
            // add in our config file
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");
            var config = builder.Build();
           
            // serilog configuration
            Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(config)
                .CreateLogger();

            services.AddSingleton(typeof(IConfigurationRoot), config); // add our config to the services collection
            services.AddSingleton<IChangeLogService, ChangeLogService>();
            services.AddSingleton<IFormsToolKitService, FormsToolKitService>();
            services.AddSingleton<IFormsToolKitPdfService, FormsToolKitPdfService>();
            services.AddSingleton<ISendMailService, SendMailService>();
            services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
            services.AddSingleton<ILoggerFactory, LoggerFactory>(); // kinda probably don't need this as we have the above line
            services.AddLogging(configure => configure.AddConsole()
                .AddSerilog()
                .AddConfiguration(config.GetSection("Logging"))
            );
            services.AddTransient<Core>();

            var serviceProvider = services.BuildServiceProvider();
            return serviceProvider;
        }


    }







1 comment:

  1. 1) Need method showing code how to log an informational message to the log file
    2) need code showing how to handle exception if the log file cannot be created on disk due to file system permissions

    It's demo code versus close to production ready code. Good examples have consideration for production ready code including error handling.

    ReplyDelete