Cysharp / ZLogger

Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.
MIT License
1.11k stars 79 forks source link

Exception output content with plain text is missing #161

Open 0x5143 opened 1 month ago

0x5143 commented 1 month ago

In unity 2022.3.20f1

try
{
    throw new Exception();
}
catch (Exception e)
{
    LogManager.Logger.ZLogError(new Exception($"Line 1", e), $"");
    LogManager.Logger.ZLogError(e, $"Line 2");
}

Exception 1 ouput:

Exception: Exception of type 'System.Exception' was thrown.
MenuPanel.StartGame () (at Assets/Scripts/Game/MenuPanel.cs:31)
Rethrow as Exception: Line 1
UnityEngine.Debug:LogException(Exception)
ZLogger.ZLoggerLogger:Log(LogLevel, EventId, VersionedLogState, Exception, Func`3)
Microsoft.Extensions.Logging.Logger:<Log>g__LoggerLog|14_0(LogLevel, EventId, ILogger, Exception, Func`3, List`1&, VersionedLogState&)
Microsoft.Extensions.Logging.Logger:Log(LogLevel, EventId, VersionedLogState, Exception, Func`3)
ZLogger.ZLoggerExtensions:ZLog(ILogger, LogLevel, EventId, Exception, ZLoggerInterpolatedStringHandler&, Object, String, String, Int32)
ZLogger.ZLoggerExtensions:ZLogError(ILogger, Exception, ZLoggerErrorInterpolatedStringHandler&, Object, String, String, Int32)
M11.<StartGame>d__2:MoveNext() (at Assets/Scripts/Game/MenuPanel.cs:35)
System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start(<StartGame>d__2&)
MenuPanel:StartGame()

Exception 2 output:

Exception: Exception of type 'System.Exception' was thrown.
MenuPanel.StartGame () (at Assets/Scripts/Game/MenuPanel.cs:31)
UnityEngine.Debug:LogException(Exception)
ZLogger.ZLoggerLogger:Log(LogLevel, EventId, VersionedLogState, Exception, Func`3)
Microsoft.Extensions.Logging.Logger:<Log>g__LoggerLog|14_0(LogLevel, EventId, ILogger, Exception, Func`3, List`1&, VersionedLogState&)
Microsoft.Extensions.Logging.Logger:Log(LogLevel, EventId, VersionedLogState, Exception, Func`3)
ZLogger.ZLoggerExtensions:ZLog(ILogger, LogLevel, EventId, Exception, ZLoggerInterpolatedStringHandler&, Object, String, String, Int32)
ZLogger.ZLoggerExtensions:ZLogError(ILogger, Exception, ZLoggerErrorInterpolatedStringHandler&, Object, String, String, Int32)
M11.<StartGame>d__2:MoveNext() (at Assets/Scripts/Game/MenuPanel.cs:36)
System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start(<StartGame>d__2&)
M11.MenuPanel:StartGame()

Line 2‘s exception output is missing.

neuecc commented 1 day ago

ZLogError routes to UnityEngine.Debug.LogException if an Exception is included, otherwise it routes to UnityEngine.Debug.LogError. https://github.com/Cysharp/ZLogger/blob/master/src/ZLogger.Unity/Assets/ZLogger.Unity/Runtime/ZLoggerUnityDebugLoggerProvider.cs#L116-L125 And it seems that UnityEngine.Debug.LogException doesn't have a method that accepts a string message, so it appears to be completely ignored. I think a workaround would be to provide "Line 2 " + e.ToString() instead of giving the Exception as is.