Wayne Brantley - 514 - Dec 9, 2013

I have Log4NetFactory as  my log manager and I have it setup to email me error messages in case anything goes wrong.  This works great. 

My question is how to control that logging.  When my service is executing it does some authorization checks and ends up throwing a “new UnauthorizedAccessException(“No User”);”.   It does this fairly often because the users session times out and then they open a drop down list on the webpage that calls the service - and of course they are no longer authenticated (through forms authentication) and I throw the above.

Of course all the above functionality works - but then I get emailed this error.   I would like to be able to easily control this.   With Log4Net I can control it by namespace, but I do not want to hide all exceptions from that namespace.   In this case, I would not want this exception bubbling up to Log4Net at all.   

I see the exception handling overrides here https://github.com/ServiceStack/ServiceStack/wiki/Error-Handling#customized-error-handling, but I do not really want to change how the exceptions are handled or override existing behavior.  Rather, I would like to filter what gets logged.

1)  The examples linked above call DtoUtils.HandleException().  That method no longer exists in the current source.  I think the documentation my be old and instead it should be DtoUtils.CreateErrorResponse().

2)  Should I just copy and paste DtoUtils.CreateErrorResponse() into my code, modify it to filter what gets logged and attach that as my handler?

3)  Is this the right place to post something like this?

I can do #2 - just seems like what gets logged should be easier to be plugged?

Wayne Brantley:

I edited the Wiki for #1 above.

The filtering of existing exceptions should be done in your logging provider. You would use the available hooks to add additional logging, there are also some overrides on AppHost like OnExceptionTypeFilter which you can override. You wont change behavior if you call the base method when overriding.

Wayne Brantley:

Well, I cannot override exception types in the log provider.  Log4Net works on namespaces, so that is a problem as I do not want to exclude everything from DtoUtils namespace?  I guess I can just copy and paste the code as my handler so I can easily filter them, but had hoped for a ‘better solution’… as I want you exact code to run, just control the logging.  Am I missing something - I am just trying to ‘get servicestack to be a little more silent when it runs!’   (Maybe let me optionally provide the logger as a parameter to CreateErrorResponse…or provide something else I could attach to to stop the logging?/)   

Wayne Brantley:

Maybe this is just a shortcoming of the logging platform I am using and I should look elsewhere?

Wayne Brantley:

And is the ‘general’ logging you have in place for CreateErrorResponse valid?  I mean on an UnauthorizedAccessException is that actually an ‘Error’ type log?  Maybe, maybe not - and that is probably the root issue.  ?  Just my thoughts.

I’m trying to think of a more flexible way to log errors, a strategy I’m adopting is to just delegate calls to virtual methods on AppHost which can be overridden (and thus intercepted), might as well do it for logging as well.

Wayne Brantley:

That would work for sure…  I am going to look at NLog too.  (What are you using for logging on your stuff?)

When I was at StackExchange, we just had a custom logger that logged to our internal https://github.com/opserver/Opserver (which was recently OSS’ed). Haven’t had the need to register a new one yet for my internal services (I use the RequestLogger tho), will evaluate the current environment soon, if it’s easy to setup opserver i’ll go with that otherwise, maybe elmah or look at creating an integration for Glimpse.

I was just going to request this. Is it still something you’re willing to do?

No longer considering anything stand-alone like Opserver, it makes more sense to integrate with logging/monitoring services on AWS and Azure.

Sorry I was referring to post 7:

I’m trying to think of a more flexible way to log errors, a strategy I’m adopting is to just delegate calls to virtual methods on AppHost which can be overridden (and thus intercepted), might as well do it for logging as well.

A virtual method to intercept logging of exceptions would be helpful. I’d use it to prevent logging of client usage errors (400s).

Do you mean logging of errors internally within ServiceStack or externally in your own Services?

Exceptions thrown within my own service.

It’s this line that would be good to be replaceable.

ok I’ve routed that Error to AppHost.OnLogError() in this commit.

Let me know if there are others you want routed (I don’t want to route all internal errors).

Thanks, that’s great.