11

Our team just moved one of our ASP.NET solutions from logging in log4net to Serilog (using iLogger) for logging. Our solution is .NET Framework 4.6. I can see Serilog configuration documentation online for setting up configuration in code as well as some documentation in appsettings.json. We have Web.config configuration files. Our old log4net configuration resided completely in the csproj files.

Is there a place for configuration for Serilog and its sinks in .NET Framework (specifically in Web.config or its own XML configuration file)? Do we have to put the configuration into the code (when we create the logger object)? Can we specify the configuration for specific controllers and models we have, and, if so, where is there documentation? I know we could specify locations, log levels, etc. for log4net for specific groups or controllers and models in log4net, but unsure how to do that for Serilog. If you got links for any of this, please point me in the right direction. Thanks.

2 Answers 2

12

After more investigating (and I can't believe I missed this earlier), the documentation here states you can edit the web.config file. In case anyone is looking for the configuration for web.config, there you go.

Sign up to request clarification or add additional context in comments.

Comments

8

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

  1. By using code only

  2. By using app.config

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.