NMQ.SerilogViewer 1.0.0.2

dotnet add package NMQ.SerilogViewer --version 1.0.0.2
                    
NuGet\Install-Package NMQ.SerilogViewer -Version 1.0.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NMQ.SerilogViewer" Version="1.0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NMQ.SerilogViewer" Version="1.0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="NMQ.SerilogViewer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NMQ.SerilogViewer --version 1.0.0.2
                    
#r "nuget: NMQ.SerilogViewer, 1.0.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NMQ.SerilogViewer@1.0.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NMQ.SerilogViewer&version=1.0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=NMQ.SerilogViewer&version=1.0.0.2
                    
Install as a Cake Tool

使用方式可参考如下方式:按需安装Serilog.Sinks.FileSerilog.Sinks.Async

    public class Log
    {
        private static LoggingLevelSwitch _fileLogSwitch = new LoggingLevelSwitch(LogEventLevel.Debug);//文件Log等级调节器
        private static LoggingLevelSwitch _displayLogSwitch = new LoggingLevelSwitch();//控件Log等级调节器
        private static Logger? _logFile;//文件Log记录器
        private static Logger? _logDisplay;//控件Log记录器

        public static void Init(IContainerProvider container)
        {
            var userService = container.Resolve<IUserService>();
            int logFileCountLimit = 20;//日志文件保留数量
            var outputTemplateNormalLog = "[{User}] [{Timestamp:yyyy-MM-dd HH:mm:ss:fff}] [{Level:u4}] [t:{ThreadId}] {Message}{NewLine}";
            var outputTemplateExceptionLog = "[{User}]  [{Timestamp:yyyy-MM-dd HH:mm:ss:fff}] [{Level:u4}] [t:{ThreadId}] {Message}{NewLine}{Exception}{NewLine}";
            _logFile = new LoggerConfiguration()
                .Enrich.With(new UserEnricher(userService))
                .Enrich.WithThreadId()
                .MinimumLevel.ControlledBy(_fileLogSwitch)
                .WriteTo.Async(config => config.File("log/xDataViewer_log_.txt", rollingInterval: RollingInterval.Day, outputTemplate: outputTemplateNormalLog, rollOnFileSizeLimit: true, fileSizeLimitBytes: 100 * 1024 * 1024, shared: true, retainedFileCountLimit: logFileCountLimit))
                //异常日志单独存一个文件夹
                .WriteTo.Logger(lc => lc.Filter.ByIncludingOnly(e =>
                e.Level >= LogEventLevel.Error && e.Exception != null)
                .WriteTo.Async(config => config.File("log/Exception/xDataViewer_Exception_log_.txt",
                            rollingInterval: RollingInterval.Day, outputTemplate: outputTemplateExceptionLog, rollOnFileSizeLimit: true, fileSizeLimitBytes: 100 * 1024 * 1024, shared: true, retainedFileCountLimit: logFileCountLimit)))
                .CreateLogger();
        }   

        /// <summary>
        /// 初始化Serilog的显示日志记录器:日志显示面板
        /// </summary>
        /// <param name="textDisplay">日志显示面板</param>
        public static void InitDisplayLogger(ILogReceived textDisplay)
        {
            var textDisplayFormart = "[{Timestamp:HH:mm:ss:fff}] [{Level:u4}] {Message}{NewLine}";
            Serilog.Formatting.Display.MessageTemplateTextFormatter tf1 = new Serilog.Formatting.Display.MessageTemplateTextFormatter(textDisplayFormart, CultureInfo.InvariantCulture);

            _logDisplay = new LoggerConfiguration()
                .MinimumLevel.ControlledBy(_displayLogSwitch)
                .WriteTo.SerilogViewerSink(tf1, textDisplay)
                .CreateLogger();
        }

        public static void Debug(string message)
        {
            _logFile!.Debug(message);
            _logDisplay?.Debug(message);

        }

        public static void Info(string message)
        {
            _logFile!.Information(message);
            _logDisplay?.Information(message);
        }

        public static void Warn(string message)
        {
            _logFile!.Warning(message);
            _logDisplay?.Warning(message);
        }

        public static void Error(string message)
        {
            _logFile!.Error(message);
            _logDisplay?.Error(message);
        }

        public static void Error(string message, Exception exception)
        {
            _logFile!.Error(exception, message);
            _logDisplay?.Error(exception, message);
        }

        public static void Fatal(string message)
        {
            _logFile!.Fatal(message);
            _logDisplay?.Fatal(message);
        }

        public static void Fatal(string message, Exception exception)
        {
            _logFile!.Fatal(exception, message);
            _logDisplay?.Fatal(exception, message);
        }

        /// <summary>
        /// 设置显示日志的日志等级
        /// </summary>
        /// <param name="dstLogLevel"></param>
        public static void SwitchDisplayLogLevel(LogEventLevel dstLogLevel) => _displayLogSwitch.MinimumLevel = dstLogLevel;

        /// <summary>
        /// 设置显示日志的日志等级
        /// </summary>
        /// <param name="dstLogLevel"></param>
        public static void SwitchFileLogLevel(LogEventLevel dstLogLevel) => _fileLogSwitch.MinimumLevel = dstLogLevel;
    }

其中UserEnricher实现如下

public class UserEnricher : ILogEventEnricher
{
    private readonly IUserService _userService;

    public UserEnricher(IUserService userService)
    {
        _userService = userService;
    }

    public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
    {
        var user = _userService.GetCurrentUserName() ?? "Unknown";

        logEvent.AddPropertyIfAbsent(
            propertyFactory.CreateProperty("User", user)
        );
    }
}
Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed.  net9.0-windows was computed.  net10.0-windows was computed. 
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.7.2

  • net6.0-windows7.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0.2 409 11/21/2025
1.0.0.1 392 11/21/2025
1.0.0 400 11/21/2025