简体   繁体   English

使用控制台作为 readkey 输入和作为 serilog 日志记录的接收器

[英]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()).我希望能够将控制台用于用户命令输入(例如: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.问题是一位同事将 serilog 作为我们的日志记录方法并将控制台用作接收器,这似乎忽略了我的 C# 代码中的每个 Console.Write() 或 Console.ReadLine() 指令。

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编辑:这是我的 json 配置文件中的 serilog 配置的摘录

"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}"
    }
  }

You could remove the configuration log for the Console and keep just the File and Debug .您可以删除 Console 的配置日志并仅保留FileDebug Remove it from Using and WriteTo arrays: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).我不确定,但也可能是控制台上的Debug日志(或 Visual Studio 上的 output window)。 If it is disturbing on the console remove it as suggested on the Console.如果它在控制台上令人不安,请按照控制台上的建议将其移除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM