简体   繁体   中英

Issue with JSNlog Exception formatting

I am having an issue with JSNlog and Nlog with formatting Exceptions coming from JSNlog. I am writing all my error logs out as JSON but when I throw an exception from javascript the system is encoding the %message property as JSON as well causing the outer JSON to be invalid.

My configuration for the Nlog.config file for this appender is:

layout='{"Enviroment":"${aspnet-application:variable=Enviroment}","CreatedOn": "${longdate:universalTime=true}", "WindowsIdentity":"${aspnet-user-identity:jsonEncode=true}","Application":"${AppName}", "Level":"${level}", ${message}}' />

My configuration for the web.config for JSNlog is:

serverSideMessageFormat=""UserAgent": "%userAgent", "SessionId": "%requestId", "URL":"%url", "UserAddress": "%userHostAddress", "Message": "%message""

When I run with this configuration it works for all normal fatal/error/warn/info/debug/trace calls but it fails with invalid JSON on any fatalException calls with the following JSON:

{"Enviroment":"Dev", "CreatedOn": "2014-09-24 23:58:42.1734", "WindowsIdentity":"XXXr","Application":"LoggingTest", "Level":"Fatal", "UserAgent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36", "SessionId": "befn1iy4mlxqowppwepf104z", "URL":"/LoggingTest/jsnlog.logger", "UserAddress": "10.247.224.202", "Message": "{"stack":"TypeError: undefined is not a function\n    at Log (http://zue-v-86a32393.vpc-am.aws.shell-cloud.com/LoggingTest/:58:16)\n    at HTMLInputElement.onclick (http:/XXXX/LoggingTest/:66:141)","message":"undefined is not a function","name":"TypeError","logData":"JS Fatal Exception"}"}

I have tried removing the quotes around the message and it then works fine for fatalException calls but fails to be valid JSON for any other logging calls as it is missing the quotes.

Does anyone know how to remedy this problem or format the exception coming out of the fatalException call?

I am the author of JSNLog.

The problem is caused by a flaw in JSNLog. When on the client you log an object, it is sent as a JSON string, such as { "x": 44 }. Note that this contains quotes. But when you log a simple string, it is sent as a simple string, without surrounding quotes and any quotes inside the string are not escaped - meaning it is incompatible with the JSON object.

This is works well in most cases where the log entry is a simple string rather than a JSON object. But it doesn't work well when you want to log a valid JSON object.

I haven't tried, this, but I assume that if you use your original set up and log a string containing a quote on the client, you will get the same error as when using fatalException.

I've decided to fix this by introducing a new property %jsonmessage, which quotes and escapes strings properly. You would then use that property in the configuration you used to make fatalException work.

I will keep you posted through comments to this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM