OnLogging Event
Lets you modify and cancel log messages.
Definition
public event LoggingHandler OnLogging
Remarks
- A working demo is in project
The Logging event is raised by JSNLog just before it sends a log message to the server side logging package (events tutorial).
This allows you to:
- Access the cookies, request headers, etc. of the log request sent from the client;
- Modify the log message, severity and logger name before they are logged;
- Stop the message from being logged at all.
Note that a simpler but less powerful way to
add information to your log messages is via the
serverSideMessageFormat
attribute of the
JSNLog
Creating and adding a logging event handler
Because you need to create and add the handler only once in the life of the application, you will want to add your code to the
Your handler has to match the LoggingHandler delegate, as defined by JSNLog. This means your method takes a LoggingEventArgs parameter and returns void:
Now you can add code to stop messages from being logged:
Or add for example all request headers to every log message:
LoggingEventArgs
An object of this type will be passed to your event handler.
Definition
public class LoggingEventArgs : FinalLogData { public bool Cancel { get; set; } } public class FinalLogData { public ILogRequest LogRequest { get; } public string FinalLogger { get; set; } public Level FinalLevel { get; set; } public string FinalMessage { get; set; } public string ServerSideMessageFormat { get; set; } }
Properties
Name | Description |
---|---|
Cancel read/write |
Initialized to false. Set to true to stop the message from being logged. |
LogRequest read only |
Cookies, headers, etc. of the log request from the client. |
FinalLogger read/write |
Name of the logger that will be sent to the server side log. |
FinalLevel read/write |
Severity level that will be sent to the server side log. |
FinalMessage read/write |
Message that will be sent to the server side log. |
ServerSideMessageFormat read |
Format set by the serverSideMessageFormat property of the JSNLog element. |
ILogRequest
Describes the original log request received from the client.
Definition
public interface ILogRequest { string Message { get; } string Logger { get; } string Level { get; } DateTime UtcDate { get; } string JsonMessage { get; } string UserAgent { get; } string UserHostAddress { get; } string RequestId { get; } string Url { get; } Dictionary<string, string> QueryParameters { get; } Dictionary<string, string> Cookies { get; } Dictionary<string, string> Headers { get; } }
Properties
Name | Description |
---|---|
Message | Original message logged on the client. |
Logger | Name of the client side logger. |
Level | Numeric severity of the log message as set on the client. |
UtcDate | Date and time in UTC when the message was generated, according to the client's clock. |
JsonMessage | Original message given to the JavaScript logger, as a valid JSON value (details). |
UserAgent | Identifies the make of the browser. |
UserHostAddress | IP address(es) of the sender of the request (details below). |
RequestId | Identifies the request for which the log message was created (details). |
Url | Url of the page on which the message was generated. |
QueryParameters | Dictionary mapping query parameter names to their values. |
Cookies | Dictionary mapping cookie names to their values. |
Headers | Dictionary mapping request header names to their values. |
UserHostAddress
When JSNLog works out the IP address where the page request was received (that is, the IP address of the user's browser), this may not be as simple as looking at the source address in the request.
If your web server sits behind a load balancer, the source of the final request to the web server is not the browser, but the load balancer. The request may also have been passed on through intermediate proxies, causing the same issue.
The most common (but non-standard) solution to this is that proxies and load balancers (such as AWS' Elastic Load Balancer) send an X-Forwarded-For request header with the IP addresses of the browser and any proxies that the request passed through, except for the final source address. JSNLog uses this request header to work out the IP address of the actual browser and the proxies and/or load balancer that the request passed through.
In that case, the "IP address" is actually a string with a comma separated list of IP addresses. First is the IP address of the browser itself, then intermediate proxies (in the order in which they were reached) and then the IP address of the load balancer if there is one:
Browser IP address, Proxy 1, ..., Proxy N [, Load Balancer]