Skip to content

Commit debd8f0

Browse files
committed
fix: 安全输出EF日志
1 parent ffb3eef commit debd8f0

File tree

1 file changed

+27
-7
lines changed
  • framework/src/Bing.Datas.EntityFramework/Logs

1 file changed

+27
-7
lines changed

framework/src/Bing.Datas.EntityFramework/Logs/EfLog.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Bing.Extensions;
88
using Bing.Helpers;
99
using Bing.Logs;
10+
using Bing.Reflection;
1011
using Bing.Uow;
1112
using Microsoft.EntityFrameworkCore.Diagnostics;
1213
using Microsoft.Extensions.Logging;
@@ -37,6 +38,7 @@ public class EfLog : ILogger
3738
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
3839
Func<TState, Exception, string> formatter)
3940
{
41+
var success = false;
4042
var config = GetConfig();
4143
var log = GetUnitOfWork()?.Log;
4244
if (log == null)
@@ -46,13 +48,31 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
4648
if (!string.IsNullOrWhiteSpace(GetUnitOfWork().TraceId))
4749
log.Tag(GetUnitOfWork()?.TraceId);
4850
log.Tag(TraceLogName);
49-
log
50-
.Caption($"执行EF操作:{formatter(state, exception)}")
51-
.Content($"工作单元跟踪号:{GetUnitOfWork()?.TraceId}")
52-
.Content($"事件ID:{eventId.Id}")
53-
.Content($"事件名称:{eventId.Name}");
54-
AddContent(state, config, log);
55-
log.Exception(exception).Trace();
51+
var caption = string.Empty;
52+
try
53+
{
54+
log
55+
.Content($"工作单元跟踪号:{GetUnitOfWork()?.TraceId}")
56+
.Content($"事件ID:{eventId.Id}")
57+
.Content($"事件名称:{eventId.Name}");
58+
AddContent(state, config, log);
59+
log.Exception(exception);
60+
caption = formatter(state, exception);
61+
success = true;
62+
}
63+
catch (Exception e)
64+
{
65+
InvokeHelper.OnInvokeException?.Invoke(e);
66+
success = false;
67+
}
68+
finally
69+
{
70+
log.Caption($"执行EF操作:{caption}");
71+
if (success)
72+
log.Trace();
73+
else
74+
log.Error();
75+
}
5676
}
5777

5878
/// <summary>

0 commit comments

Comments
 (0)