Global exception handler

0
1

For Wisej app, I am noticing following in app log file

Connection ID “10736581521851821452”, Request ID “80002d8d-0002-9500-b63f-84710c7967bb”: An unhandled exception was thrown by the application.

System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake.
at System.Net.WebSockets.ManagedWebSocket.ThrowIfEOFUnexpected(Boolean throwOnPrematureClosure)
at System.Net.WebSockets.ManagedWebSocket.EnsureBufferContainsAsync(Int32 minimumRequiredBytes, CancellationToken cancellationToken, Boolean throwOnPrematureClosure)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TResult](Memory`1 payloadBuffer, CancellationToken cancellationToken)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token)
at System.Threading.Tasks.ValueTask`1.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state)
— End of stack trace from previous location —
at Wisej.Core.HttpHandler.ProcessWebSocketRequest(HttpContext context)
at Microsoft.AspNetCore.Builder.Extensions.UsePathBaseMiddleware.InvokeCore(HttpContext context, PathString matchedPath, PathString remainingPath)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()

so I would like to implement global exception handler, which can do it by adding to programs.cs, which will show me error page, but I would like to log it to file using serilog nuget package, any suggestions how to implement it?

app.UseExceptionHandler(

                options =>

                {

                    options.Run(

                        async context =>

                        {

                            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

                            context.Response.ContentType = “text/html”;

                            var ex = context.Features.Get<IExceptionHandlerFeature>();

                            if (ex != null)

                            {

                                var err = $”<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace}”;

                                await context.Response.WriteAsync(err).ConfigureAwait(false);

                            }

                        });

                }

            );

  • You must to post comments
0
0

Attach to the https://docs.wisej.com/api/wisej.web/general/application#threadexception event.

  • You must to post comments
0
0

This is a simple configuration of Serilog in code (Program.cs):

var logFile = (string) Application.Configuration.Settings.logFile;

Log.Logger = new LoggerConfiguration()
.WriteTo.File(
logFile,
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 7,
shared: true,
encoding: Encoding.Default, // SBCSCodePageEncoding
outputTemplate: “{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}]  {Message:lj}{NewLine}{Exception}”
)
.CreateLogger();

Or place the configuration in Web.config and read it from there.

Then log wherever you want:

Log.Information(“Something happend.”);

Log.Error(“Something bad happend.”);

 

See also https://github.com/serilog/serilog/wiki

Cheers, Gerhard

 

 

 

 

 

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.