.NET core logging [serilog]

Logging using Serilog

Per serilog:  Serilog provides diagnostic logging to files, the console, and elsewhere. It is easy to set up, has a clean API, and is portable between recent .NET platforms.


  1. Nuget package: Serilog.Extensions.Logging.File

  2. In Startup.cs, in Configure add ILoggerFactory loggerFactory and below code

  3.  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
            {
                
                //Serilog.Extensions.Logging.File
                loggerFactory.AddFile("Logs/MyApp-{Date}.txt");
    
                // .NET logging
    
                loggerFactory = LoggerFactory.Create(builder => builder.AddFilter("Microsoft", LogLevel.Warning)
                .AddFilter("System", LogLevel.Warning)
                .AddFilter("LoggingConsoleApp.Program", LogLevel.Debug)
                .AddEventLog()
                .AddDebug());
            }
    

  4. Inject ILogger in your class and log as below

  5.  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    public class UserProducer : IUserProducer
        {
            private readonly ILogger<UserProducer> _logger;
    
            public UserProducer()
            {
    
            }
            public  UserProducer (ILogger<UserProducer> logger)
            {
                _logger = logger;
            }
            public async void Produce(string message)
            {
                var config = new ProducerConfig { BootstrapServers = "localhost:9094" };
    
                using (var producer = new ProducerBuilder<Null, string>(config).Build())
                {
                    try
                    {
                        _logger.LogInformation($"Trying to deliver message {message}");
                        var result = await producer.ProduceAsync("send-message", new Message<Null, string> { Value = "hi" });
                        _logger.LogInformation($"Delivered message {result.Value} to {result.TopicPartitionOffset}");
                    }
                    catch (ProduceException<Null, string> e)
                    {
                        _logger.LogError($"Delivery of message failed {e.Error.Reason}");
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
    

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...