74 lines
2.3 KiB
C#
74 lines
2.3 KiB
C#
using System;
|
|
using Serilog;
|
|
using XP.Common.Logging.Interfaces;
|
|
|
|
namespace XP.Common.Logging.Implementations
|
|
{
|
|
/// <summary>
|
|
/// Serilog日志服务实现(适配ILoggerService接口)| Serilog logger service implementation (adapts ILoggerService interface)
|
|
/// </summary>
|
|
public class SerilogLoggerService : ILoggerService
|
|
{
|
|
private ILogger _logger;
|
|
|
|
/// <summary>
|
|
/// 构造函数:初始化全局日志实例 | Constructor: initialize global logger instance
|
|
/// </summary>
|
|
public SerilogLoggerService()
|
|
{
|
|
_logger = Log.Logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 私有构造函数:用于模块标记 | Private constructor: for module tagging
|
|
/// </summary>
|
|
private SerilogLoggerService(ILogger logger)
|
|
{
|
|
_logger = logger ?? Log.Logger;
|
|
}
|
|
|
|
public void Debug(string message, params object[] args)
|
|
{
|
|
_logger.Debug(message, args);
|
|
}
|
|
|
|
public void Info(string message, params object[] args)
|
|
{
|
|
_logger.Information(message, args);
|
|
}
|
|
|
|
public void Warn(string message, params object[] args)
|
|
{
|
|
_logger.Warning(message, args);
|
|
}
|
|
|
|
public void Error(Exception ex, string message, params object[] args)
|
|
{
|
|
_logger.Error(ex, message, args);
|
|
}
|
|
|
|
public void Fatal(Exception ex, string message, params object[] args)
|
|
{
|
|
_logger.Fatal(ex, message, args);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 手动指定模块名 | Manually specify module name
|
|
/// </summary>
|
|
public ILoggerService ForModule(string moduleName)
|
|
{
|
|
if (string.IsNullOrEmpty(moduleName)) return this;
|
|
return new SerilogLoggerService(_logger.ForContext("SourceContext", moduleName));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 自动使用类型全名作为模块名 | Automatically use type full name as module name
|
|
/// </summary>
|
|
/// <typeparam name="T">类型参数 | Type parameter</typeparam>
|
|
public ILoggerService ForModule<T>()
|
|
{
|
|
var typeName = typeof(T).FullName ?? typeof(T).Name;
|
|
return new SerilogLoggerService(_logger.ForContext("SourceContext", typeName));
|
|
}
|
|
}
|
|
} |