1

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.

4
  • I dont think you can write files to an arbitrary location with winui3. stackoverflow.com/questions/61250141/… Commented Mar 28 at 20:25
  • thank you @mxmissile it's now creating the file and writing logs into it, had give it a file path with right permissions to write as described in the thread you shared. But I don't see any console logs Commented Mar 28 at 22:21
  • To directly answer your title - Serilog is a .NET library - of course you can use it anywhere you can use .NET. There may be specific nuances of certain sinks on certain platforms - things like permissions and app restrictions will apply to Serilog just like it would to any other code running in an app on the same platform. Commented Mar 29 at 1:11
  • @mason thank you, your comment makes perfect sense, got the idea now. Since it was not working and most of examples on www are for ASP.NET I thought, may be it does not work for WinUI Apps. Commented Mar 29 at 22:02

1 Answer 1

0

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.

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

1 Comment

I was able to see logs being printed in Debug/Output window when I added Serilog.Sinks.Debug. Thank you Andrew Keepcoding. Also I did not try the OutputType as this works for my usecase.

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.