none
.NET Core logging RRS feed

  • Question

  • Hi,

    I am new to .NET Core development and having a look at the new Microsoft.Extensions.Logging.

    The goal is to write the log to disk file but I am not finding any information on how to do it using this library. I read some information that the option to write to  file is not available.

    Could anyone please help on this?

    Thanks

    Angelo

    Tuesday, July 12, 2016 9:57 AM

Answers

  • Hi StockDev,

    >> The goal is to write the log to disk file but I am not finding any information on how to do it using this library. I read some information that the option to write to file is not available.

    According to your description, it seems that you want to write the log to files.  We could use Serilog.Sinks.RollingFile to  achieve it..

    Firstly, please install the Serilog.Extensions.Logging NuGet package into web or console app

    Install-Package Serilog.Extensions.Logging
    
    install-package Serilog.Sinks.RollingFile

    Secondly, I create a simple console app demo as below for your reference

    static void Main(string[] args)
    
            {
    
                var log = new LoggerConfiguration()
    
              .MinimumLevel.Debug()
    
              .WriteTo.RollingFile(@"D:\log\log.txt", retainedFileCountLimit: 7)
    
              .CreateLogger();
    
    
                log.Error("Test");
    
                Console.WriteLine("OK");
    
                Console.ReadKey();
    
            }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, July 13, 2016 5:29 AM
    Moderator

All replies

  • Hi StockDev,

    >> The goal is to write the log to disk file but I am not finding any information on how to do it using this library. I read some information that the option to write to file is not available.

    According to your description, it seems that you want to write the log to files.  We could use Serilog.Sinks.RollingFile to  achieve it..

    Firstly, please install the Serilog.Extensions.Logging NuGet package into web or console app

    Install-Package Serilog.Extensions.Logging
    
    install-package Serilog.Sinks.RollingFile

    Secondly, I create a simple console app demo as below for your reference

    static void Main(string[] args)
    
            {
    
                var log = new LoggerConfiguration()
    
              .MinimumLevel.Debug()
    
              .WriteTo.RollingFile(@"D:\log\log.txt", retainedFileCountLimit: 7)
    
              .CreateLogger();
    
    
                log.Error("Test");
    
                Console.WriteLine("OK");
    
                Console.ReadKey();
    
            }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, July 13, 2016 5:29 AM
    Moderator
  • Hi StockDev,

    Here is an article from MSDN magazine that looks like it has a few examples.

    https://msdn.microsoft.com/en-us/magazine/mt694089.aspx

    Cheers

    Wednesday, July 13, 2016 12:38 PM
  • A couple people have posted about using Serilog instead of the built in logging, which is fine.

    I also wanted to add that you can use the internal logging and add an extension to it to enable File logging, via Serilog. So this sounds like the same thing but it isn't.

    So yes you can completely switch away from the built in logging, or you can use it, but just add an extension to it for File logging. Logging with ASP.NET Core is definitely a little different a little confusing. Read how: https://github.com/serilog/serilog-extensions-logging-file


    Azure power user for 4 years and .NET developer for 14 years. Founder and CEO of Stackify APM - Advanced application monitoring for Azure

    Tuesday, February 14, 2017 1:14 PM