Search code examples
c#serilog

How do I write HelloWorld to a console with Serilog app.config from command line


I have a vendor who told me they are supporting the App.config file in Serilog so I can change the min level of logs as their (beta) product is filling up the file system and crashing it.

I don't think they have implemented it properly, because I don't see any App.config file anywhere in their release base. My understanding is that they need to change their loggers to also point to App.config otherwise no matter what I put in there it won't work.

So I thought I would try to test a simple file to write hello world with an app.config file and see what I can get it to do. This worked just fine when I had the logger defined in the code, but once I added the app.config file, the logger stopped working.

Sitting in a folder on my desktop, I have the Serilog dll files from my vendor and my 2 files also pasted below, hw.cs and app.config. I'm compiling and running it from the command line as my test system doesn't have Visual Studio or internet access.

This works: csc.exe hw.cs /r:Serilog.dll /r:Serilog.Sinks.Console.dll /r:Serilog.Settings.AppSettings.dll

When I run my hw.exe file, I only see the console.writeline comments (Step 1, Step 2 etc) and not the logger comments.

Do I need to do something special so my hw.cs file knows where the App.config file is located? Do I have the correct dll?

Thanks for any help on this.

The code hw.cs is below:

using System;
using Serilog;

namespace SerilogExample
{
    class HelloWorld
    {
        static void Main()
        {
            Console.WriteLine("Step 1");
            Log.Logger = new LoggerConfiguration()
                .ReadFrom.AppSettings()
                .CreateLogger();

            Console.WriteLine("Step 2");
            Log.Information("Hello world");

            Console.WriteLine("Step 3");
            int a = 10, b = 0;
            Log.Debug("Display a and b: {A} and {B}", a, b);
            Console.WriteLine("Step 4");

            Log.Error("---pretend something went wrong");
            Console.WriteLine("Step 5");

            Log.CloseAndFlush();
        }
    }
}

App.config is below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="serilog:minimum-level" value="Debug" />
    <add key="serilog:using:Console" value="Serilog.Sinks.Console" />
    <add key="serilog:write-to:Console" />
  </appSettings>
 </configuration>

Solution

  • You don't have to do anything special in your code above, but in .NET the App.config file must be in the same folder of your application executable, and it must be named NameOfYourApp.exe.config for it to work.

    In your case, the file should be called hw.exe.config for the framework to load it.


    Update: Try enabling the SelfLog to see if Serilog is having issues itself:

    Serilog.Debugging.SelfLog.Enable(
        msg => Console.WriteLine("Error from Serilog: {0}", msg));
    

    This is what I see running your code with the hw.exe.config on the same folder:

    enter image description here


    ps: Serilog has two main options to configure settings through a file: App.config (via Serilog.Settings.AppSettings) and appsettings.json (via Serilog.Settings.Configuration). Modern apps use the latter one.