64 lines
2.3 KiB
C#
64 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Security.Cryptography;
|
|
using System.IO;
|
|
using Serilog;
|
|
using Serilog.Events;
|
|
using log4net;
|
|
|
|
namespace SERVER.LIB
|
|
{
|
|
public static class LogHelp_
|
|
{
|
|
private static object locks = new object();
|
|
private static string path = Directory.GetCurrentDirectory();
|
|
public static string time = string.Empty;
|
|
private static ILog log = LogManager.GetLogger("All");
|
|
public static void Init()
|
|
{
|
|
if (time == string.Empty) {
|
|
lock (locks)
|
|
{
|
|
if (time == string.Empty)
|
|
{
|
|
time = DateTime.Now.ToString("yyyy_MM_dd");
|
|
string pathurl = path + "\\App_Data\\Log\\XC.log";
|
|
Directory.CreateDirectory(path + "\\App_Data\\Log");
|
|
Log.Logger = new LoggerConfiguration()
|
|
.MinimumLevel.Warning()
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
|
.Enrich.FromLogContext()
|
|
.WriteTo.Console()
|
|
.WriteTo.File(pathurl, // 日志文件名
|
|
outputTemplate: // 设置输出格式,显示详细异常信息
|
|
@"{Timestamp:yyyy-MM-dd HH:mm-ss.fff }[{Level:u3}] {Message:lj}{NewLine}{Exception}",
|
|
rollingInterval: RollingInterval.Day, // 日志按day保存
|
|
rollOnFileSizeLimit: true, // 限制单个文件的最大长度
|
|
encoding: Encoding.UTF8, // 文件字符编码
|
|
retainedFileCountLimit: 10, // 最大保存文件数
|
|
fileSizeLimitBytes: 5000 * 1024) // 最大单个文件长度 5m
|
|
.CreateLogger();
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Error(string msg)
|
|
{
|
|
Init();
|
|
Log.Error(msg);
|
|
log.Error(msg);
|
|
}
|
|
|
|
public static void Warning(string msg)
|
|
{
|
|
Init();
|
|
Log.Warning(msg);
|
|
}
|
|
|
|
}
|
|
}
|