Custom Logger for Elmah producing error when trying to read the logs - elmah.mvc

We are trying to implement a custom logger for Elmah to log errors in a RavenDB database in an MVC5 application.
The Logging works perfectly but when we try to read the logs we get an exception for all properties of the Error Class that are of type NameValueCollection with the following error :
Cannot populate list type
System.Collections.Specialized.NameValueCollection. Path
'Error.ServerVariables'.
Any assistance would be greatly appreciated

Related

Spring boot common logging

I'm very new to the spring boot..
I understood that Spring Boot uses Commons Logging for all internal logging.
logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.hibernate=error
after adding this into application.properties file, application logs all the output and exceptions in the console..
so I have a requirement to print the logs only when the system throws the error not for the success response..
so I have a requirement to print the logs only when the system throws the error not for the success response..
The implementation of this requirement IMO heavily depends on your system
From the word "response" I understand that you're working with some kind of controller (like in spring mvc). But the controller method is only an entry point to your backend.
What if the controller calls the service that logs something (message logA), then (after logging) it calls another service that again logs something (message logB) and then in turn calls, dao to call the datatbase?
If, say, DB threw an error, the logA and logB messages are already logged and you can't "take that back".
So, in general, you can log that there was an error by explicitly catching the exception in controller and logging the error, or using Controller advice to intercept and log the exceptions "globally".
When you reach the point where you log the message you can log it with severity, say, ERROR and the logging framework will log it as long as its configured to log messages of that level from that logger.

Google Cloud Platform Log Viewer logs truncate jsonPayload message for error log

The logs I'm viewing in the Log Viewer are truncating the "message" part of the payload, but the "exception" part of the payload doesn't have a limit and shows the entire stacktrace. These logs are for exceptions caught in my service running in Google Cloud.
The service is built with Spring Boot using SLF4J as a logger factory. I use SLF4J's LoggerFactory to create a logger based on the class where it's invoked and when an exception is caught, I log using the logger's error("Exception thrown processing this message: ${message.data}", exception) where message is of type com.google.pubsub.v1.PubsubMessage and exception is a Throwable of type java.lang.Exception.
I had the same problem.
The only way is to jump to Logs Explore and watch the log.

passing a Spring Exception to a Thymeleaf page

Having added the error.html Thymeleaf template to replace the default error page on Spring how can I get access to the exception on that page?
So I have custom exception thrown on some controller. How to access that exception on the Thymeleaf page?
I want this to work globally, so any controller thrown exception should be accessible.
Case 1: For customized error pages with spring default error info
There are some predefined objects in Thymeleaf to show error information, e.g. ${error}, ${exception} and so on. These objects can be used in your customized error page.
This article will help you more. Custom Error Page with Thymeleaf
Case 2: For customized error pages with customized error info
There are #ControllerAdvice and #ExceptionHandler used to handle exceptions in controllers globally. A certain exception can be add to Modal's attribute and therefore accessed in thymeleaf templates. However, please be noted that the solution does not work in Webflux.
The blog shows details about the solution. Exception Handling in Spring MVC

Force Spring to always report exceptions as JSON

How do I force Spring to always convert uncaught exception to JSON instead of HTML page?
This is what I get when request is made from Chrome's REST client plugin:
{
timestamp: 1425041457798
status: 404
error: "Not Found"
exception: "com.some.my.Exception"
message: "/rrr does not exist"
path: "/test/rrr"
}
But this is what I get when I access it from browser or from Jersey API (you see parsed HTML):
Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.Fri Feb 27 13:37:27 CET 2015There was an unexpected error (type=Not Found, status=404).No message available
I know this can be done somehow by setting the request headers but I want JSON response to be the only variant.
First we need add some exception resolver for exception that trows inside controllers. I prefer extend ResponseEntityExceptionHandler and add own method, but there is a great article on spring.io: http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
After that if you still get following page you can override org.springframework.boot.autoconfigure.web.ErrorController. There are an example: BasicErrorController. But this works only if you use spring boot application with embedded container. For example, if you will create war file form spring boot project and will deploy under tomcat, you will get standard tomcat error page.
So this mean that ErrorController is not common solution.
In my case i throws exceptions inside filters, that why /error page shown. So solution will be write own filter that converts exceptions to JSON representation. This solution should work for every container/server, and you can get more information about exception.

How to configure spring HandlerExceptionResolver to handle NullPointerException thrown in jsp?

From a jsp is thrown a NullPointerException for example using <% null.toString(); %>
This exception is not handled by the HandlerExceptionResolver, but thrown to the web container(tomcat) and converted into a code 500 error.
How can I configure spring to get that error in my HandlerExceptionResolver ?
Details:
Spring can be configured to handle exceptions thrown inside Controllers, but not exceptions thrown by view.
Of course i can resolve the NullPointerException, but i want to design a solution that will gracefully resolve any possible problem on the web application in order to display a user friendly message to the user.
See the HandlerInterceptor interface instead. You'll want the afterCompletion method. You can then intercept the response and then set the appropriate header information to redirect to a container-configured error web page. You're right that Spring doesn't have this functionality, this is going to have to be specified by the web.xml which determines which codes map to which pages.
I have not worked with this particular bit of the spring framework, but the docs say
"Interface to be implemented by objects than can resolve exceptions thrown during handler mapping or execution, in the typical case to error views. Implementors are typically registered as beans in the application context.
Error views are analogous to the error page JSPs, but can be used with any kind of exception including any checked exception, with potentially fine-granular mappings for specific handlers."
so I'd imagine that given that NullPointer extends RuntimeException the framework isn't designed to catch it. Is there a reason the exception(s) can't be handled in the controller directly?

Resources