9

I can not find any resources for installing Serilog in an ASP.Net 4.7.1 WebApi project. Can someone help me out? There are a ton of .Net Core resources but that does not help.

Jim Kiely
  • 365
  • 2
  • 6
  • 24

3 Answers3

20

Install required NuGet packeges, open the Package Manager Console and type

Install-Package Serilog
Install-Package Serilog.Sinks.File

Create new static class with name logger that will have Serilog configuration

public static class Logger
{
    private static readonly ILogger _errorLogger;

    static Logger()
    {
        _errorLogger = new LoggerConfiguration()
            .WriteTo.File(HttpContext.Current.Server.MapPath("~/logs/log-.txt"), rollingInterval: RollingInterval.Day)
            .CreateLogger();
    }

    public static void LogError(string error)
    {
        _errorLogger.Error(error);
    }
}

Use logger class when you want to log error as below

Logger.LogError("Test error log!");
ElasticCode
  • 7,311
  • 2
  • 34
  • 45
  • 2
    I got too caught up in the tutorials and how they setup serilog. This is the right answer for my project. I'm using an exception filter and this works perfectly. – Jim Kiely Feb 25 '19 at 16:51
  • agree, keep it simple – rickthehat Apr 22 '22 at 23:27
  • I think this is not needed anymore. Serilog has a "Log" static class, with a Logger property to initialize it. So you can initialize it in the Startup class and just use it everywhere. – Romias Jun 20 '22 at 15:44
3

There are two ways one can configure Serilog in .net framework 4.7.2:

  1. By using code only

  2. By using app.config

Following nuget packages needed to be installed:

    Install-Package Serilog
    Install-Package Serilog.Settings.AppSettings
    Install-Package Serilog.Sinks.File

1st Way (By Using code only):

Make a static serilogclass:

public static class SerilogClass
    {
        public static readonly Serilog.ILogger _log;
        static SerilogClass()
        {
            _log = new LoggerConfiguration().
                    MinimumLevel.Debug().                    
                    WriteTo.File(@Environment.GetEnvironmentVariable("LocalAppData") + "\\Logs\\Logs1.log").
                    CreateLogger();
        }

    }

Note: @Environment.GetEnvironmentVariable("LocalAppData") will save logfile into appdata folder

Initialize and Use the SerilogClass in program.cs

class Program
    {       
        static readonly Serilog.ILogger log = SerilogClass._log;
        
        static void Main(string[] args)
        {            
             log.Debug("This is serialog Example");
             log.Debug("This is serialog Example2");
        }
            
    }       
    

2nd Way(By using app.config):

Make a static serilogclass:

public static class SerilogClass
    {       
        public static readonly Serilog.ILogger _log;        
        static SerilogClass()
        {
            _log = new LoggerConfiguration().
                    ReadFrom.AppSettings().
                    CreateLogger();
        }

    }
    

Initialize and Use the SerilogClass in program.cs

 class Program
        {       
            static readonly Serilog.ILogger log = SerilogClass._log;
            
            static void Main(string[] args)
            {            
                 log.Debug("This is serialog Example using app.config");
                 log.Debug("This is serialog Example2 using app.config");
            }
                
        }   

We need too add <appSettings></appSettings> section to define all settings which we were doing via code in 1st way

App.config:

<configuration> 
    <configSections></configSections>
    <appSettings>
        <add key="serilog:minimum-level" value="Debug"/>
        <add key="serilog:using:File" value="Serilog.Sinks.File" />     
        <add key="serilog:write-to:File.path" value="C:\Logs\LogSerilog.txt" />
        <add key="serilog:write-to:File.shared" value="true" />
        <add key="serilog:write-to:File.rollOnFileSizeLimit" value="true" />
        <add key="serilog:write-to:File.fileSizeLimitBytes" value="2000" />
    </appSettings>
    <startup></startup>
    <runtime></runtime>
</configuration>
Brijesh Ray
  • 779
  • 8
  • 15
1

In my case, i simply made a static serilog class and initialized it in main class to use it (for my console application .net framework 4.7.2):

 public static class SerilogClass
    {
        public static readonly Serilog.ILogger _log;
        static SerilogClass() 
        {
        _log= new LoggerConfiguration().
                MinimumLevel.Debug().
                WriteTo.File(@"D:\MyPoject\Logs.log").
                CreateLogger();
        }
                 
    }

Program.cs:

 class Program
    {
        static readonly Serilog.ILogger log3 = SerilogClass._log;
        static void Main(string[] args)
        {
            log3.Debug("This is serialog Example");
        }
}
Brijesh Ray
  • 779
  • 8
  • 15