Windows service - read serilog configuration from a config file

58 views Asked by At

I have a service that I want to configure the Logger(serilog) before it actually starts.

So in the program.cs I have this :

static class Program
{
    static void Main()
    {
        myservice.StartLoging();

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
        {
            new myservice()
        };
        ServiceBase.Run(ServicesToRun);
    }
}

So in myservice.cs I have the following code :

    public static void StartLoging()
    {
        Log.Logger = new LoggerConfiguration()                            
                        .WriteTo.File("C:\\Logs\\myservicelogs.log")
                        .MinimumLevel.Debug()
                        .CreateLogger();
        Log.Debug("Start logging");
    }

And this works. But what I have to do to read the LoggerConfiguration from a config file ? I put the serilog configuration in Settings.settings file. But didn't work. So my Settings.settings looked like :

<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="... CurrentProfile="(Default)" GeneratedClassNamespace="myservice.Properties" GeneratedClassName="Settings">
 <Profiles />
<Settings>
<Setting Name="serilog:minimum-level" Type="System.String" Scope="User">
<Value Profile="(Default)">Error</Value>
</Setting>
<Setting Name="serilog:using:File" Type="System.String" Scope="User">
<Value Profile="(Default)">Serilog.Sinks.File</Value>...

It looks like it isn't read from the settings.settings file the configuration of the serilog.

0

There are 0 answers