修复bug
This commit is contained in:
79
WarehouseMgr/Tnb.WarehouseMgr/ServiceLoggerBase`1.cs
Normal file
79
WarehouseMgr/Tnb.WarehouseMgr/ServiceLoggerBase`1.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System.Text;
|
||||
using JNPF;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SqlSugar;
|
||||
using Tnb.WarehouseMgr.Entities;
|
||||
|
||||
namespace Tnb.WarehouseMgr
|
||||
{
|
||||
public class ServiceLoggerBase<TService> : BaseWareHouseService
|
||||
{
|
||||
protected static Dictionary<string, object> s_elevatorMap = new();
|
||||
private static readonly Lazy<Task> initializationTask;
|
||||
|
||||
|
||||
static ServiceLoggerBase()
|
||||
{
|
||||
initializationTask = new Lazy<Task>(InitializeAsync);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static async Task InitializeAsync()
|
||||
{
|
||||
|
||||
ConnectionStringsOptions connectionOpts = App.GetConfig<ConnectionStringsOptions>("ConnectionStrings", true);
|
||||
ConnectionConfig cfg = new()
|
||||
{
|
||||
ConfigId = connectionOpts.ConfigId,
|
||||
ConnectionString = connectionOpts.ConnectString,
|
||||
DbType = DbType.PostgreSQL,
|
||||
IsAutoCloseConnection = true,
|
||||
};
|
||||
SqlSugarScope context = new(cfg);
|
||||
|
||||
s_elevatorMap = await context.Queryable<WmsElevatorH>().ToDictionaryAsync(x => x.elevator_id, x => x.elevator_code);
|
||||
|
||||
}
|
||||
public static Task InitializationTask => initializationTask.Value;
|
||||
|
||||
|
||||
|
||||
protected ILogger Logger => LoggerFactory.Create(builder => builder.AddFile($"{AppContext.BaseDirectory}/logs/{this.GetType().Name}{DateTime.Now:yyyyMMdd}.log", cfgOpts =>
|
||||
{
|
||||
|
||||
//cfgOpts.DateFormat = "yyyy-MM-dd HH:mm:ss.fff";
|
||||
cfgOpts.MessageFormat = (logMsg) =>
|
||||
{
|
||||
Span<char> span = logMsg.LogLevel.ToString().ToCharArray();
|
||||
StringBuilder sb = new();
|
||||
_ = sb.Append($"{span[..4]} ");
|
||||
_ = sb.Append($"{logMsg.LogName} ");
|
||||
_ = sb.Append($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} ");
|
||||
_ = sb.Append($"#{logMsg.EventId.Id} ");
|
||||
_ = sb.Append(logMsg.Message + " ");
|
||||
_ = sb.Append(logMsg.Exception?.ToString());
|
||||
return sb.ToString();
|
||||
};
|
||||
|
||||
})).CreateLogger<TService>();
|
||||
}
|
||||
|
||||
public static class CustomLoggerExtenstions
|
||||
{
|
||||
public static void Debug(this ILogger logger, string message, params object[] parameters)
|
||||
{
|
||||
logger.Debug(message, parameters);
|
||||
}
|
||||
|
||||
public static void Information(this ILogger logger, string message, params object[] parameters)
|
||||
{
|
||||
logger.LogInformation(message, parameters);
|
||||
}
|
||||
|
||||
public static void Error(this ILogger logger, string message, Exception ex, params object[] parameters)
|
||||
{
|
||||
logger.LogError(ex, message, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user