protected override void WriteMessage(LogLevel logLevel, string logName, EventId eventId, string message, Exception exception) { var dateTime = DateTime.UtcNow; var fieldsDictionary = new Dictionary <string, string>(Options.Fields); foreach (var kvp in GetStructuredScopeInformation()) { // Overwrite later earlier scope info with more recent. fieldsDictionary[kvp.Key] = kvp.Value?.ToString(); } var splunkEventData = new SplunkEventData( PayloadTransformer.Transform( new LogData { Timestamp = dateTime.FormatForSplunk(Options.TimestampFormat), CategoryName = logName, Scope = GetTextualScopeInformation(), Level = logLevel, Event = eventId, Message = message, Exception = exception }), dateTime.FormatForSplunk(null), // JsonEventCollector must be in Epoch sec's format. Options.Host, Options.Index, Options.Source, Options.SourceType, fieldsDictionary); LoggerProcessor.EnqueueMessage(JsonConvert.SerializeObject(splunkEventData)); }
public void EnsureDefaultDateFormatAllowsJsonDeserialization() { // Arrange string configXml = @" <jsnlog serverSideMessageFormat=""{ 'utcDate': '%utcDate', 'utcDateServer': '%utcDateServer', 'date': '%date', 'dateServer': '%dateServer' }""></jsnlog>"; XmlElement xe = Utils.ConfigToXe(configXml); // Act List <LoggerProcessor.LogData> actual = LoggerProcessor.ProcessLogRequestExec(_json1, "my browser", "12.345.98.7", _dtServerUtc, "http://mydomain.com/main", "", xe); string messageToBeLogged = actual.FirstOrDefault().Message; var javaScriptSerializer = new JavaScriptSerializer(); var datesBag1 = javaScriptSerializer.Deserialize <DatesBag>(messageToBeLogged); TestDatesEqual(datesBag1.utcDate, _dtFirstLogUtc); TestDatesEqual(datesBag1.utcDateServer, _dtServerUtc); TestDatesEqual(datesBag1.date, _dtFirstLog); TestDatesEqual(datesBag1.dateServer, _dtServer); var datesBag2 = Newtonsoft.Json.JsonConvert.DeserializeObject <DatesBag>(messageToBeLogged); TestDatesEqual(datesBag2.utcDate, _dtFirstLogUtc); TestDatesEqual(datesBag2.utcDateServer, _dtServerUtc); TestDatesEqual(datesBag2.date, _dtFirstLog); TestDatesEqual(datesBag2.dateServer, _dtServer); }
public void ProcessRequest(HttpContext context) { string userAgent = context.Request.UserAgent; string userHostAddress = context.Request.UserHostAddress; DateTime serverSideTimeUtc = DateTime.UtcNow; string url = context.Request.Url.AbsolutePath; string requestId = JSNLog.Infrastructure.RequestId.GetFromRequest(); string json; using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding)) { json = reader.ReadToEnd(); } LoggerProcessor.ProcessLogRequest(json, userAgent, userHostAddress, serverSideTimeUtc, url, requestId); // Send dummy response. That way, the log request will not remain "pending" // in eg. Chrome dev tools. // // This must be given a MIME type of "text/plain" // Otherwise, the browser may try to interpret the empty string as XML. // When the user uses Firefox, and right clicks on the page and chooses "Inspect Element", // then in that debugger's console it will say "no element found". // See // http://www.acnenomor.com/307387p1/how-do-i-setup-my-ajax-post-request-to-prevent-no-element-found-on-empty-response // http://stackoverflow.com/questions/975929/firefox-error-no-element-found/976200#976200 HttpResponse Response = context.Response; Response.ContentType = "text/plain"; Response.Write(""); }
private void RunTestHttp( string httpMethod, string origin, string configXml, string json, string requestId, string userAgent, string userHostAddress, DateTime serverSideTimeUtc, string url, int expectedResponseCode, Dictionary <string, string> expectedResponseHeaders, List <LogEntry> expectedLogEntries) { // Arrange LogResponse response = new LogResponse(); TestLogger logger = new TestLogger(); CommonTestHelpers.SetConfigCache(configXml, logger); // Act LoggerProcessor.ProcessLogRequest( json, new LogRequestBase(userAgent, userHostAddress, requestId, url, null, null, null), serverSideTimeUtc, httpMethod, origin, response); // Assert Assert.Equal(expectedResponseCode, response.StatusCode); TestLogEntries(expectedLogEntries, logger.LogEntries); TestResponseHeaders(expectedResponseHeaders, response.Headers); }
private void RunTest(string configXml, string json, string requestId, string userAgent, string userHostAddress, DateTime serverSideTimeUtc, string url, IEnumerable <LoggerProcessor.LogData> expected) { XmlElement xe = Utils.ConfigToXe(configXml); // Act List <LoggerProcessor.LogData> actual = LoggerProcessor.ProcessLogRequestExec(json, userAgent, userHostAddress, serverSideTimeUtc, url, requestId, xe); TestLogDatasEqual(expected, actual); }
private void ProcessRequest(HttpContext context) { var headers = ToDictionary(context.Request.Headers); string urlReferrer = headers.SafeGet("Referer"); string url = context.Request.GetDisplayUrl(); var logRequestBase = new LogRequestBase( userAgent: headers.SafeGet("User-Agent"), userHostAddress: context.GetUserIp(), requestId: context.GetLogRequestId(), url: (urlReferrer ?? url).ToString(), queryParameters: ToDictionary(context.Request.Query), cookies: ToDictionary(context.Request.Cookies), headers: headers); DateTime serverSideTimeUtc = DateTime.UtcNow; string httpMethod = context.Request.Method; string origin = headers.SafeGet("Origin"); Encoding encoding = HttpHelpers.GetEncoding(headers.SafeGet("Content-Type")); string json; using (var reader = new StreamReader(context.Request.Body, encoding)) { json = reader.ReadToEnd(); } var response = new LogResponse(); LoggerProcessor.ProcessLogRequest(json, logRequestBase, serverSideTimeUtc, httpMethod, origin, response); // Send dummy response. That way, the log request will not remain "pending" // in eg. Chrome dev tools. // // This must be given a MIME type of "text/plain" // Otherwise, the browser may try to interpret the empty string as XML. // When the user uses Firefox, and right clicks on the page and chooses "Inspect Element", // then in that debugger's console it will say "no element found". // See // http://www.acnenomor.com/307387p1/how-do-i-setup-my-ajax-post-request-to-prevent-no-element-found-on-empty-response // http://stackoverflow.com/questions/975929/firefox-error-no-element-found/976200#976200 ToAspNet5Response(response, context.Response); context.Response.ContentType = "text/plain"; context.Response.ContentLength = 0; }
private void RunTest(string configXml, string json, string requestId, string userAgent, string userHostAddress, DateTime serverSideTimeUtc, string url, IEnumerable <LogData> expected) { XmlElement xe = CommonTestHelpers.ConfigToXe(configXml); // Act var jsnlogConfiguration = XmlHelpers.DeserialiseXml <JsnlogConfiguration>(xe); List <FinalLogData> actual = LoggerProcessor.ProcessLogRequestExec( json, new LogRequestBase(userAgent, userHostAddress, requestId, url, null, null, null), serverSideTimeUtc, jsnlogConfiguration); TestLogDatasEqual(expected, actual, serverSideTimeUtc); }
public void ProcessRequest(HttpContext context) { var logRequestBase = new LogRequestBase( userAgent: context.Request.UserAgent, userHostAddress: context.GetUserIp(), requestId: context.GetLogRequestId(), url: (context.Request.UrlReferrer ?? context.Request.Url).ToString(), queryParameters: Utils.ToDictionary(context.Request.QueryString), cookies: ToDictionary(context.Request.Cookies), headers: Utils.ToDictionary(context.Request.Headers)); DateTime serverSideTimeUtc = DateTime.UtcNow; string httpMethod = context.Request.HttpMethod; string origin = context.Request.Headers["Origin"]; string json; using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding)) { json = reader.ReadToEnd(); } var logResponse = new LogResponse(); LoggerProcessor.ProcessLogRequest(json, logRequestBase, serverSideTimeUtc, httpMethod, origin, logResponse); // Send dummy response. That way, the log request will not remain "pending" // in eg. Chrome dev tools. // // This must be given a MIME type of "text/plain" // Otherwise, the browser may try to interpret the empty string as XML. // When the user uses Firefox, and right clicks on the page and chooses "Inspect Element", // then in that debugger's console it will say "no element found". // See // http://www.acnenomor.com/307387p1/how-do-i-setup-my-ajax-post-request-to-prevent-no-element-found-on-empty-response // http://stackoverflow.com/questions/975929/firefox-error-no-element-found/976200#976200 HttpResponse httpResponse = context.Response; ToHttpResponse(logResponse, httpResponse); httpResponse.ContentType = "text/plain"; httpResponse.ClearContent(); httpResponse.Write(""); }
protected override void WriteMessage(LogLevel logLevel, string logName, EventId eventId, string message, Exception exception) { // Queue log message var splunkEventData = PayloadTransformer.Transform( new LogData { Timestamp = DateTime.UtcNow.FormatForSplunk(Options.TimestampFormat), CategoryName = logName, Scope = GetTextualScopeInformation(), Level = logLevel, Event = eventId, Message = message, Exception = exception }).ToString(); LoggerProcessor.EnqueueMessage(splunkEventData); }
public void InternalError() { string configXml = @" <jsnlog></jsnlog> "; XmlElement xe = Utils.ConfigToXe(configXml); // Act List <LoggerProcessor.LogData> actual = LoggerProcessor.ProcessLogRequestExec(_json4, "my browser", "12.345.98.7", _dtServerUtc, "http://mydomain.com/main", "", xe); // Assert Assert.AreEqual(1, actual.Count); Assert.AreEqual(Constants.JSNLogInternalErrorLoggerName, actual.ElementAt(0).LoggerName); }
public void InternalError() { string configXml = @" <jsnlog></jsnlog> "; XmlElement xe = CommonTestHelpers.ConfigToXe(configXml); // Act var jsnlogConfiguration = XmlHelpers.DeserialiseXml <JsnlogConfiguration>(xe); List <FinalLogData> actual = LoggerProcessor.ProcessLogRequestExec( _json4, new LogRequestBase("my browser", "12.345.98.7", "", "http://mydomain.com/main", null, null, null), _dtServerUtc, jsnlogConfiguration); // Assert Assert.Equal(1, actual.Count); Assert.Equal(Constants.JSNLogInternalErrorLoggerName, actual.ElementAt(0).FinalLogger); }
private void RunTestHttp( string httpMethod, string origin, string configXml, string json, string requestId, string userAgent, string userHostAddress, DateTime serverSideTimeUtc, string url, int expectedResponseCode, NameValueCollection expectedResponseHeaders, List <LogEntry> expectedLogEntries) { // Arrange TestHttpResponse response = new TestHttpResponse(); TestLogger logger = new TestLogger(); XmlElement xe = Utils.ConfigToXe(configXml); // Act LoggerProcessor.ProcessLogRequest(json, userAgent, userHostAddress, serverSideTimeUtc, url, requestId, httpMethod, origin, response, logger, xe); // Assert Assert.AreEqual(expectedResponseCode, response.StatusCode); TestLogEntries(expectedLogEntries, logger.LogEntries); TestResponseHeaders(expectedResponseHeaders, response.Headers); }
public void ProcessRequest(HttpContext context) { string userAgent = context.Request.UserAgent; string userHostAddress = context.Request.UserHostAddress; DateTime serverSideTimeUtc = DateTime.UtcNow; string url = context.Request.Url.AbsolutePath; string requestId = JSNLog.Infrastructure.RequestId.GetFromRequest(); string json; using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding)) { json = reader.ReadToEnd(); } LoggerProcessor.ProcessLogRequest(json, userAgent, userHostAddress, serverSideTimeUtc, url, requestId); // Send dummy response. That way, the log request will not remain "pending" // in eg. Chrome dev tools. HttpResponse Response = context.Response; Response.Write(""); }
public void EnsureDefaultDateFormatAllowsJsonDeserialization() { // Arrange string configXml = @" <jsnlog serverSideMessageFormat=""{ 'utcDate': '%utcDate', 'utcDateServer': '%utcDateServer', 'date': '%date', 'dateServer': '%dateServer' }""></jsnlog>"; XmlElement xe = CommonTestHelpers.ConfigToXe(configXml); // Act var jsnlogConfiguration = XmlHelpers.DeserialiseXml <JsnlogConfiguration>(xe); List <FinalLogData> actual = LoggerProcessor.ProcessLogRequestExec( _json1, new LogRequestBase("my browser", "12.345.98.7", "http://mydomain.com/main", "", null, null, null), _dtServerUtc, jsnlogConfiguration); string messageToBeLogged = actual.FirstOrDefault().FinalMessage; var datesBag1 = LogMessageHelpers.DeserializeJson <DatesBag>(messageToBeLogged); TestDatesEqual(datesBag1.utcDate, _dtFirstLogUtc); TestDatesEqual(datesBag1.utcDateServer, _dtServerUtc); TestDatesEqual(datesBag1.date, _dtFirstLog); TestDatesEqual(datesBag1.dateServer, _dtServer); var datesBag2 = Newtonsoft.Json.JsonConvert.DeserializeObject <DatesBag>(messageToBeLogged); TestDatesEqual(datesBag2.utcDate, _dtFirstLogUtc); TestDatesEqual(datesBag2.utcDateServer, _dtServerUtc); TestDatesEqual(datesBag2.date, _dtFirstLog); TestDatesEqual(datesBag2.dateServer, _dtServer); }
public StdOutLoggerProvider(IOptions <StdOutLoggerOptions> options) { _options = options.Value; _loggers = new ConcurrentDictionary <string, Internals.StdOutLogger>(); _loggerProcessor = new LoggerProcessor(); }