C# Serilog配置和使用

1.安装NuGet安装
C# Serilog配置和使用_第1张图片

2.LogSerilog.cs类代码如下:
using Serilog;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormPro
{
public class LogSerilog
{
private LogSerilog(string name)
{
string SerilogOutputTemplate = “Date:{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}LogLevel:{Level}{NewLine}Message:{Message}{NewLine}{Exception}” + new string(‘-’, 50) + “{NewLine}{NewLine}”;
log = new LoggerConfiguration().MinimumLevel.Debug()
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug).WriteTo.Async(t => t.File( " L o g / n a m e / n a m e . l o g " , r o l l i n g I n t e r v a l : R o l l i n g I n t e r v a l . D a y , r e t a i n e d F i l e C o u n t L i m i t : 24 , b u f f e r e d : f a l s e , s h a r e d : t r u e , o u t p u t T e m p l a t e : S e r i l o g O u t p u t T e m p l a t e ) ) ) . W r i t e T o . L o g g e r ( l g = > l g . F i l t e r . B y I n c l u d i n g O n l y ( p = > p . L e v e l = = L o g E v e n t L e v e l . I n f o r m a t i o n ) . W r i t e T o . A s y n c ( t = > t . F i l e ( "Log/{name}/{name}_.log", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 24, buffered: false, shared: true, outputTemplate: SerilogOutputTemplate))) .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.Async(t => t.File( "Log/name/name.log",rollingInterval:RollingInterval.Day,retainedFileCountLimit:24,buffered:false,shared:true,outputTemplate:SerilogOutputTemplate))).WriteTo.Logger(lg=>lg.Filter.ByIncludingOnly(p=>p.Level==LogEventLevel.Information).WriteTo.Async(t=>t.File(“Log/{name}/{name}.log", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 24, buffered: false, shared: true, outputTemplate: SerilogOutputTemplate)))
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.Async(t => t.File($"Log/{name}/{name}
.log”, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 24, buffered: false, shared: true, outputTemplate: SerilogOutputTemplate)))
.CreateLogger();

    //log = new LoggerConfiguration().MinimumLevel.Debug()
    //.WriteTo.File($"Log/.txt", rollingInterval: RollingInterval.Day)
    //.CreateLogger();
}

private static Dictionary _serilogDic = new Dictionary();

public static LogSerilog GetInstance(string LogName)
{
    lock (_serilogDic)
    {
        if (_serilogDic.Keys.Contains(LogName))
        {
            return _serilogDic[LogName];
        }
        else
        {
            LogSerilog instance = new LogSerilog(LogName);
            _serilogDic.Add(LogName, instance);
            return instance;
        }
    }

}

private ILogger log;
public ILogger GetILogger
{
    get { return log; }
}

}

}
3.Form中使用Serilog
using Serilog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormPro
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public ILogger _serilog
{
get { return LogSerilog.GetInstance(“MCS”).GetILogger; }
}
private void Form1_Load(object sender, EventArgs e)
{
_serilog.Error( " M C S L I N K M A N A G E R S E R V I C E S T O P " ) ; s e r i l o g . I n f o r m a t i o n ( "MCS LINK MANAGER SERVICE STOP "); _serilog.Information( "MCSLINKMANAGERSERVICESTOP");serilog.Information("MCS LINK MANAGER dataService Start ");
}
}

你可能感兴趣的:(c#,开发语言)