serilogwinui-3winuiserilog-sinks-fileserilog-sinks-console

Does Serilog support WinUI 3.0 Apps?


I am new to WinUI development, I created a new simple WinUI 3.0 app (using Viusal Studio template Blank App, Packaged (WinUI 3 in desktop)

I also installed the following

$ dotnet add package Serilog
$ dotnet add package Serilog.Sinks.Console
$ dotnet add package Serilog.Sinks.File

This is how my App.xaml.cs constructor looks like.

public App()
{
    this.InitializeComponent();

    Log.Logger = new LoggerConfiguration()
        .WriteTo.Console()
        .WriteTo.File("logs\\myapp.txt", rollingInterval: RollingInterval.Day)
        .CreateLogger();

    Log.Information("Application Starting");
}

I don't see any logs on the console or file being created too. What could I be doing wrong? Are there some special permissions I need to request etc. Appreciate any tips or debugging suggestions for me, thank you.


Solution

  • Changing the OutputType from WinExe to Exe should do the trick:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <!--<OutputType>WinExe</OutputType>-->
        <OutputType>Exe</OutputType>
    ...
    

    If you just want to see the logs for debug, you should also consider using the Serilog.Sinks.Debug sink. This sink writes logs to the Visual Studio Output window.