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.

3 Answers 3

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!"); 
Sign up to request clarification or add additional context in comments.

3 Comments

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.
agree, keep it simple
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.
4

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> 

Comments

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"); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.