Skip to content

In Blazor WebAssembly, use ILoggerFactory for unhandled exceptions #19533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SteveSandersonMS opened this issue Mar 3, 2020 · 2 comments
Closed
Assignees
Labels
area-blazor Includes: Blazor, Razor Components Done This issue has been fixed feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly

Comments

@SteveSandersonMS
Copy link
Member

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.

@SteveSandersonMS SteveSandersonMS added area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly labels Mar 3, 2020
@MarekPokornyOva
Copy link

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
Copy link
Member Author

Done in #19606

@SteveSandersonMS SteveSandersonMS added Done This issue has been fixed and removed Working labels Mar 13, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Apr 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components Done This issue has been fixed feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly
Projects
None yet
Development

No branches or pull requests

4 participants