可以更改serilog中的颜色吗? [英] Is it possible to change colors in serilog?

查看:111
本文介绍了可以更改serilog中的颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将Serilog集成到我的点网核心项目中.它确实运行良好,但我使用了深色主题,有些日志很难阅读.例如:

这是我初始化Serilog的方式:

 字符串环境= Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");LoggerConfiguration loggerConfig = new LoggerConfiguration();如果(环境==生产")loggerConfig.MinimumLevel.Information();loggerConfig.MinimumLevel.Override("Microsoft.AspNetCore",LogEventLevel.Warning).Enrich.FromLogContext().WriteTo.Console().WriteTo.File("Logs/app.log"); 

有什么办法可以改变颜色,例如使黑色原木变成白色?

解决方案

是的,使用控制台接收器时更改颜色的方法是通过

可以在配置接收器时指定主题:

  .WriteTo.Console(主题:AnsiConsoleTheme.Code) 

在撰写本文时,可以使用以下内置主题:

  • ConsoleTheme.None -无样式
  • SystemConsoleTheme.Literate -样式设置为使用所有Windows/上支持的 System.Console 着色模式来复制 Serilog.Sinks.Literate .NET目标;这是未指定主题时的默认设置
  • SystemConsoleTheme.Grayscale -仅使用灰色,白色和黑色阴影的主题
  • AnsiConsoleTheme.Literate -识字"字样的ANSI 16色版本.主题;我们希望将其更新为使用256色,以便将来更加精致
  • AnsiConsoleTheme.Grayscale -灰度"的ANSI 256色版本.主题
  • AnsiConsoleTheme.Code -一个受ANSI 256色Visual Studio Code启发的主题

添加新主题非常简单;可以在 SystemConsoleThemes 解决方案

Yes, the way to change colors when using the Console sink is through themes. You can try one of the built-in ones, or create your own.

The Console sink will colorize output by default:

Themes can be specified when configuring the sink:

    .WriteTo.Console(theme: AnsiConsoleTheme.Code)

The following built-in themes are available as of this writing:

  • ConsoleTheme.None - no styling
  • SystemConsoleTheme.Literate - styled to replicate Serilog.Sinks.Literate, using the System.Console coloring modes supported on all Windows/.NET targets; this is the default when no theme is specified
  • SystemConsoleTheme.Grayscale - a theme using only shades of gray, white, and black
  • AnsiConsoleTheme.Literate - an ANSI 16-color version of the "literate" theme; we expect to update this to use 256-colors for a more refined look in future
  • AnsiConsoleTheme.Grayscale - an ANSI 256-color version of the "grayscale" theme
  • AnsiConsoleTheme.Code - an ANSI 256-color Visual Studio Code-inspired theme

Adding a new theme is straightforward; examples can be found in the SystemConsoleThemes and AnsiConsoleThemes classes.

这篇关于可以更改serilog中的颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆