admin管理员组

文章数量:1354705

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.

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.

Share Improve this question edited Mar 29 at 23:12 Andrew KeepCoding 14.1k2 gold badges21 silver badges37 bronze badges asked Mar 28 at 19:49 SoftHumanSoftHuman 133 bronze badges 4
  • I dont think you can write files to an arbitrary location with winui3. stackoverflow/questions/61250141/… – mxmissile 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 – SoftHuman 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. – mason 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. – SoftHuman Commented Mar 29 at 22:02
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: Does Serilog support WinUI 30 AppsStack Overflow