In Blazor WebAssembly, use ILoggerFactory for unhandled exceptions
SteveSandersonMS opened this issue · 2 comments
SteveSandersonMS commented
Currently, WebAssemblyRenderer's override for HandleException simply calls Console.Error.WriteLine. This means there's no way to plug in any customer exception logging feature.
We should change this to use whatever ILoggerFactory is registered in DI.
MarekPokornyOva commented
This would be very appretiated.
Nowadays, I have to workaround that:
public static void Install(IUnhandledExceptionHandler unhandledExceptionHandler)
{
TextWriter tw = Console.Error;
Console.SetError(new CatchTextWriter(tw,unhandledExceptionHandler,false));
}
class CatchTextWriter:TextWriter
{
readonly TextWriter _innerWriter;
readonly IUnhandledExceptionHandler _unhandledExceptionHandler;
readonly bool _omitGeneralErrorMessage;
public CatchTextWriter(TextWriter innerWriter,IUnhandledExceptionHandler unhandledExceptionHandler,bool omitGeneralErrorMessage)
{
_innerWriter=innerWriter;
_unhandledExceptionHandler=unhandledExceptionHandler;
_omitGeneralErrorMessage=omitGeneralErrorMessage;
}
public override void WriteLine(object value)
{
if (value is Exception ex)
{
UnhandledExceptionContext ctx = new UnhandledExceptionContext(ex);
_unhandledExceptionHandler.HandleException(ctx);
if (ctx.Handled)
return;
}
_innerWriter.Write(value);
}
public override void WriteLine(string value)
{
if (_omitGeneralErrorMessage&&string.Equals("Unhandled exception rendering component:",value,StringComparison.Ordinal))
return;
_innerWriter.WriteLine(value);
}
#region just forward
#endregion just forward
SteveSandersonMS commented
Done in #19606