Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the console as readkey input AND as sink for serilog logging

I would like to be able to use the console for user command inputs (ex: console.Readkey()). The problem is that a colleague incorporated serilog as our logging method and uses the console as a sink which seems to ignore every Console.Write() or Console.ReadLine() instruction in my C# code.

Is there a way around this without having to create a 2nd console instance?

EDIT: here is the extract of the serilog config in my json configuration file

"Logging": {
"Using": [
  "Serilog.Sinks.Console",
  "Serilog.Sinks.File",
  "Serilog.Sinks.Debug"
],
"MinimumLevel": "Debug",
"WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "restrictedToMinimumLevel": "Debug",
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
    }
  },
  {
    "Name": "File",
    "Args": {
      "path": "xxxxxxxx.log",
      "rollingInterval": "Day",
      "rollOnFileSizeLimit": true,
      "retainedFileCountLimit": 10,
      "fileSizeLimitBytes": 10240000,
      "restrictedToMinimumLevel": "Debug",
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
    }
  },
  {
    "Name": "Debug",
    "Args": {
      "restrictedToMinimumLevel": "Debug",
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
    }
  }
like image 855
snus74 Avatar asked Dec 01 '25 03:12

snus74


1 Answers

You could remove the configuration log for the Console and keep just the File and Debug. Remove it from Using and WriteTo arrays:

"Logging": {
    "Using": [
        "Serilog.Sinks.File",
        "Serilog.Sinks.Debug"
    ],
    "MinimumLevel": "Debug",
    "WriteTo": [
        {
            "Name": "File",
            "Args": {
                "path": "xxxxxxxx.log",
                "rollingInterval": "Day",
                "rollOnFileSizeLimit": true,
                "retainedFileCountLimit": 10,
                "fileSizeLimitBytes": 10240000,
                "restrictedToMinimumLevel": "Debug",
                "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
            }
        },
        {
            "Name": "Debug",
            "Args": {
                "restrictedToMinimumLevel": "Debug",
                "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
            }
        }
    ]
}

I am not sure but maybe the Debug log on Console as well (or on the output window on visual studio). If it is disturbing on the console remove it as suggested on the Console.

like image 98
Felipe Oriani Avatar answered Dec 02 '25 16:12

Felipe Oriani