question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Correct way to log exception in interceptor?

See original GitHub issue

Hey all. How should a catch-all exception logger look like? The code below does what I want, but I’m not sure if returning null is the best idea. I’ve checked the sample here, but there is no exception handling https://github.com/grpc/grpc-dotnet/blob/1f67ee2d00e60281dfc78a072b41fc575e0d1ce3/examples/Logger/Server/ServerLoggerInterceptor.cs.

I’m using .NET Framework and including gRPC and Microsoft.Extensions.Hosting packages so I don’t get the nice stuff that .NET Core has by default. Thanks!

    public override Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod<TRequest, TResponse> continuation)
    {
        try
        {
            return base.UnaryServerHandler(request, context, continuation);
        }
        catch (Exception exception)
        {
            _logger.LogError(exception.ToString());
            return null;
        }
    }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
wyepezcommented, Mar 10, 2022

core…and the implementation is pretty much the same as yours:

public override Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod<TRequest, TResponse> continuation)
    {
        try
        {
            return base.UnaryServerHandler(request, context, continuation);
        }
        catch (Exception exception)
        {
            _logger.LogError(exception.ToString());
            throw;
        }
    }

Adding async/await do the trick

1reaction
kle622commented, Mar 30, 2020

@thdotnet yup. If you are using .net core, it is as simple as

services.AddGrpc(options = 
{
    options.Interceptors.Add<TInterceptor>();
});

If you are using .net framework, it is a bit more involved

_server = new Server
{
    Services = 
    {
        Proto.Service.BindService(ServiceInstance).Intercept(InterceptorInstance);
    }
    Ports = ...
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Log4j Logger for Struts 2 Exception Interceptor - java
To enable logging of the exceptions being handled by the Struts 2 framework you must specify some parameter values in struts.xml.
Read more >
Get Exception Details Inside Mule Interceptor
I was thinking is there any way to get the exception details inside this method by using may be MuleEvent object. Or is...
Read more >
Logging and intercepting database operations - EF6
Starting with Entity Framework 6, anytime Entity Framework sends a command to the database this command can be intercepted by application ...
Read more >
Logging Exceptions in Java - Loggly
A look at common approaches to exception handling and logging in Java. ... Choosing one option over the other depends mostly on how...
Read more >
Class ExceptionMappingInterceptor
This interceptor forms the core functionality of the exception handling feature. Exception handling allows you to map an exception to a result code, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found