当前位置: 首页 > article >正文

C#-关于日志的功能扩展

目录

一、日志Sink(接收器)

二、Trace追踪实现日志

三、日志滚动


一、日志Sink(接收器)

安装NuGet包:Serilog

Sink有很多种,这里介绍两种:

                Console接收器(安装Serilog.Sinks.Console);

                File接收器(安装Serilog.Sinks.File);

MinimumLevel:最小记录级别

rollingInterval:生成日志文件周期

outputTemplate:输出日志模板

继承ILogEventSink接口实现 Emit:当Sink器接收到新日志时触发

通过该接口将接收器接收的日志添加进内部日志集合

将该接口实现类实例化对象通过WriteTo.Sink(myEventSink)与Logger绑定

实现 ILogEventSink接口示例:

 public List<string> Logs = new List<string>();

 private readonly ITextFormatter _formatter=
            new MessageTemplateTextFormatter("Message:{Message}  [{Level}]  Location:{FilePath}[{LineNumber}]");
 public void Emit(LogEvent logEvent)
 {
   if (logEvent != null)
    {
        var textWriter=new StringWriter();
        _formatter.Format(logEvent, textWriter);
        Logs.Add(textWriter.ToString());
    }
 }

 Main程序:

  MyEventSink myEventSink = new MyEventSink();
  string path = "Logs\\Error\\.txt";
  string outputTemplate = "{NewLine}Date: {Timestamp:yyyy-MM-dd HH:mm:ss.fff}\tLevel: {Level}\tCallName: {SourceContext}->{MemberName}"
       + "{NewLine}Path: {FilePath}[{LineNumber}]"
       + "{NewLine}Message: {Message}";
  Log.Logger = new LoggerConfiguration()
              .Enrich.FromLogContext()//记录相关上下文信息
              .MinimumLevel.Debug()
              .WriteTo.Sink(myEventSink)
              .WriteTo.Logger(log => log.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error)
                                  .WriteTo.File(path, rollingInterval: RollingInterval.Day, outputTemplate: outputTemplate))
              .WriteTo.Console()
              .CreateLogger();
  Log.Warning("*****************Warning***************");
  Log.Logger.Information("*******************Info****************");
  Log.Logger.CallError<Test>("#####################Error##################");
  foreach (string str in myEventSink.Logs)
  {
       Console.WriteLine(str);
  }
    static class LogExtension
    {
        public static void CallError<T>(this ILogger logger, string message,
            [CallerMemberName] string meberName = "",
            [CallerFilePath] string filepath = "",
            [CallerLineNumber] int lineNum = 0)
            => logger.ForContext<T>()
            .ForContext("MemberName", meberName)
            .ForContext("FilePath", filepath)
            .ForContext("LineNumber", lineNum)
            .Error(message);
        public static void CallError<T>(this ILogger logger, Exception e, string message,
            [CallerMemberName] string meberName = "",
            [CallerFilePath] string filepath = "",
            [CallerLineNumber] int lineNum = 0)
            => logger.ForContext<T>()
            .ForContext("MemberName", meberName)
            .ForContext("FilePath", filepath)
            .ForContext("LineNumber", lineNum)
            .Error(e, message);
    }

二、Trace追踪实现日志

继承抽象类TraceListener,重写方法TraceEvent

注意:添加监听对象Trace.Listeners.Add(this);

        public override void TraceEvent(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, string? message)
        {
            switch (eventType)
            {
                case TraceEventType.Error:
                    Log.Logger.CallError<MyTraceListen>(message);
                    break;
                case TraceEventType.Warning:
                    Log.Logger.Warning(message);
                    break;
                case TraceEventType.Information:
                    Log.Logger.Information(message);
                    break;
                default:
                    break;
            }
        }

三、日志滚动

通过ObservableCollection类的CollectionChanged事件实现日志自动滚动到底部:

        集合改变触发事件,更改附加属性AutoScroll值,值更改触发CallBack将日志滚动到底部;

注意:MouseEnterMouseLeave两事件的响应原因:查看日志时,防止日志自动滚动到底部;

        <DataGrid attach:ScrollHelper.AutoScroll="{Binding AutoScroll}"
                  AutoGenerateColumns="False"
                  CanUserAddRows="False"
                  CanUserDeleteRows="False"
                  CanUserReorderColumns="False"
                  CanUserResizeColumns="False"
                  CanUserResizeRows="False"
                  CanUserSortColumns="False"
                  ItemsSource="{Binding LogService.Logs}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseEnter">
                    <i:InvokeCommandAction Command="{Binding MouseEnterCommand}" />
                </i:EventTrigger>
                <i:EventTrigger EventName="MouseLeave">
                    <i:InvokeCommandAction Command="{Binding MouseLeaveCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Time}" Header="时间" />
                <DataGridTextColumn Binding="{Binding Lev}" Header="级别" />
                <DataGridTextColumn Binding="{Binding Message}" Header="信息">
                    <DataGridTextColumn.ElementStyle>
                        <Style>
                            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                            <Setter Property="TextBlock.TextAlignment" Value="Left" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow" BasedOn="{StaticResource DataGridRowStyle}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Lev}" Value="Error">
                            <Setter Property="Foreground" Value="Red"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Lev}" Value="Warn">
                            <Setter Property="Foreground" Value="Orange"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
        public LogService LogService { get; set; }=LogService.GetInstance();

        private bool _autoScroll;
        public bool AutoScroll
        {
            get { return _autoScroll; }
            set => SetProperty(ref _autoScroll, value);
        }

        [RelayCommand]
        public void MouseEnter()
        {
            LogService._logs.CollectionChanged -= Scroll;
        }

        [RelayCommand]
        public void MouseLeave()
        {
            LogService._logs.CollectionChanged += Scroll;
        }

        private void Scroll(object sender, NotifyCollectionChangedEventArgs e)
        {
            AutoScroll = !AutoScroll;
        }
        public MainWinViewModel()
        {
            LogService.OpenListen();
            LogService._logs.CollectionChanged += Scroll;
        }


http://www.kler.cn/a/146709.html

相关文章:

  • NLP论文速读(EMNLP 2024)|动态奖励与提示优化来帮助语言模型的进行自我对齐
  • freertos任务调度学习
  • 详细解析STM32 GPIO引脚的8种模式
  • 环形缓冲区 之 STM32 串口接收的实现
  • 使用 GoZero 实现读取绩效表格 Excel 并打分
  • std::sort的底层原理(混合排序算法)
  • Linux的基本指令(四)
  • SpringBoot应用手册
  • docker启动容器失败,然后查看日志,docker logs查看容器出现报错:
  • 【Spring Boot】Swagger的常用注解
  • 评测|PolarDB MySQL 版 Serverless
  • 【Axure高保真原型】3D金字塔图_移入显示数据标签
  • C++ 文件和流、异常处理、动态内存、预处理器
  • 嵌入式设备视频编码比较:H.264、H.265、MPEG-2和MJPG
  • Feign接口请求返回异常 no suitable HttpMessageConvert found for response type
  • 卷积神经网络(CNN)车牌识别
  • csgo/steam搬砖项目新手教程
  • 6.12找树左下角的值(LC513-M)
  • TCP知识点
  • PT里如何针对某个模块设置false path
  • 【初始前后端交互+原生Ajax+Fetch+axios+同源策略+解决跨域】
  • OpenAI神秘项目Q-star曝光,人类永生或灭绝,将在我们有生之年发生
  • Python---练习:使用Python函数编写通讯录系统
  • Mindomo Desktop for Mac免费思维导图软件,助您高效整理思维
  • Linux系统---僵尸进程、孤儿进程
  • Linux网络——数据链路层