private void SendLogRecord(String source, TraceEventType eventType, int id, String message, TraceEventCache eventCache) { var logrec = new LogRecord { TimeUtc = DateTime.UtcNow, LoggerName = source, Message = message, Server = Environment.MachineName, ApplicationPath = AppDomain.CurrentDomain.BaseDirectory, ProcessId = process.Id, ProcessName = process.ProcessName, Identity = Thread.CurrentPrincipal.Identity.Name }; if (id != 0) { logrec.Message = String.Format(":{0} {1}", id, message); } if (eventCache == null) { var thread = Thread.CurrentThread; logrec.ThreadId = thread.ManagedThreadId; } else { int threadId; if (Int32.TryParse(eventCache.ThreadId, out threadId)) { logrec.ThreadId = threadId; } } if (eventType <= TraceEventType.Critical) { logrec.LogLevel = LogRecord.ELogLevel.Critical; } else if (eventType <= TraceEventType.Error) { logrec.LogLevel = LogRecord.ELogLevel.Error; } else if (eventType <= TraceEventType.Warning) { logrec.LogLevel = LogRecord.ELogLevel.Warning; } else if (eventType <= TraceEventType.Information) { logrec.LogLevel = LogRecord.ELogLevel.Info; } else if (eventType <= TraceEventType.Verbose) { logrec.LogLevel = LogRecord.ELogLevel.Debug; } else { logrec.LogLevel = LogRecord.ELogLevel.Trace; } connector.SendLogRecord(logrec); }
void context_Error(object sender, EventArgs e) { var ctx = HttpContext.Current; var request = ctx.Request; var response = ctx.Response; var ex = ctx.Server.GetLastError(); const String duplicateKey = "__llddiag_alreadyserved"; if (ctx.Items.Contains(duplicateKey)) { return; } ctx.Items.Add(duplicateKey, true); var buffer = new StringBuilder(maxHttpDataLength, maxHttpDataLength); var logrec = new LogRecord { Identity = Thread.CurrentPrincipal.Identity.Name, LoggerName = "ASP.NET", LogLevel = LogRecord.ELogLevel.Error, ProcessId = process.Id, ProcessName = process.ProcessName, Server = Environment.MachineName, ThreadId = Thread.CurrentThread.ManagedThreadId, TimeUtc = DateTime.UtcNow, AdditionalFields = new Dictionary <String, Object> { { "Url", request.RawUrl }, { "HttpStatusCode", response.StatusCode }, { "ClientIP", request.UserHostAddress }, { "RequestData", ExtractHeaders(buffer, request.Headers) }, { "ResponseData", ExtractHeaders(buffer, response.Headers) } } }; try { logrec.ApplicationPath = HttpRuntime.AppDomainAppPath; } catch { logrec.ApplicationPath = AppDomain.CurrentDomain.BaseDirectory; } if (ex != null) { logrec.ExceptionType = ex.GetType().FullName; logrec.ExceptionMessage = ex.Message; logrec.ExceptionAdditionalInfo = ex.StackTrace; } connector.SendLogRecord(logrec); }
public override void ProcessEvent(WebBaseEvent raisedEvent) { var logrec = new LogRecord { AdditionalFields = new Dictionary <String, Object>() }; this.AddBasicDataFields(logrec, raisedEvent); if (raisedEvent is WebManagementEvent) { this.AddWebProcessInformationDataFields(logrec, ((WebManagementEvent)raisedEvent).ProcessInformation); } if (raisedEvent is WebRequestEvent) { this.AddWebRequestInformationDataFields(logrec, ((WebRequestEvent)raisedEvent).RequestInformation); } if (raisedEvent is WebBaseErrorEvent) { this.AddExceptionDataFields(logrec, ((WebBaseErrorEvent)raisedEvent).ErrorException); } if (raisedEvent is WebAuditEvent) { this.AddWebRequestInformationDataFields(logrec, ((WebAuditEvent)raisedEvent).RequestInformation); } if (raisedEvent is WebRequestErrorEvent) { logrec.LogLevel = LogRecord.ELogLevel.Error; this.AddWebRequestInformationDataFields(logrec, ((WebRequestErrorEvent)raisedEvent).RequestInformation); this.AddWebThreadInformationDataFields(logrec, ((WebRequestErrorEvent)raisedEvent).ThreadInformation); } if (raisedEvent is WebErrorEvent) { logrec.LogLevel = LogRecord.ELogLevel.Error; this.AddWebRequestInformationDataFields(logrec, ((WebErrorEvent)raisedEvent).RequestInformation); this.AddWebThreadInformationDataFields(logrec, ((WebErrorEvent)raisedEvent).ThreadInformation); } if (raisedEvent is WebAuthenticationSuccessAuditEvent) { logrec.AdditionalFields.Add("UserToSignIn", ((WebAuthenticationSuccessAuditEvent)raisedEvent).NameToAuthenticate); } if (raisedEvent is WebAuthenticationFailureAuditEvent) { logrec.AdditionalFields.Add("UserToSignIn", ((WebAuthenticationFailureAuditEvent)raisedEvent).NameToAuthenticate); } if (raisedEvent is WebViewStateFailureAuditEvent) { logrec.LogLevel = LogRecord.ELogLevel.Error; this.AddExceptionDataFields(logrec, ((WebViewStateFailureAuditEvent)raisedEvent).ViewStateException); } connector.SendLogRecord(logrec); }
public override void OnException(ExceptionContext filterContext) { if (filterContext.ExceptionHandled) { return; } const String duplicateKey = "__llddiag_alreadyserved"; if (filterContext.HttpContext.Items.Contains(duplicateKey)) { return; } filterContext.HttpContext.Items.Add(duplicateKey, true); var request = filterContext.HttpContext.Request; var response = filterContext.HttpContext.Response; var ex = filterContext.Exception; var buffer = new StringBuilder(maxHttpDataLength, maxHttpDataLength); var logrec = new LogRecord { ExceptionType = ex.GetType().FullName, ExceptionMessage = ex.Message, ExceptionAdditionalInfo = ex.StackTrace, Identity = Thread.CurrentPrincipal.Identity.Name, LoggerName = String.Format("MVC.{0}.{1}", filterContext.RouteData.Values["controller"], filterContext.RouteData.Values["action"]), LogLevel = LogRecord.ELogLevel.Error, ProcessId = process.Id, ProcessName = process.ProcessName, Server = Environment.MachineName, ThreadId = Thread.CurrentThread.ManagedThreadId, TimeUtc = DateTime.UtcNow, AdditionalFields = new Dictionary <String, Object> { { "Url", request.RawUrl }, { "HttpStatusCode", response.StatusCode }, { "ClientIP", request.UserHostAddress }, { "RequestData", ExtractHeaders(buffer, request.Headers) }, { "ResponseData", ExtractHeaders(buffer, response.Headers) } } }; try { logrec.ApplicationPath = HttpRuntime.AppDomainAppPath; } catch { logrec.ApplicationPath = AppDomain.CurrentDomain.BaseDirectory; } connector.SendLogRecord(logrec); }
public override void OnException(HttpActionExecutedContext context) { const String duplicateKey = "__llddiag_alreadyserved"; if (context.Request.Properties.ContainsKey(duplicateKey)) { return; } context.Request.Properties.Add(duplicateKey, true); var request = context.Request; var ex = context.Exception; var buffer = new StringBuilder(maxHttpDataLength, maxHttpDataLength); var logrec = new LogRecord { ApplicationPath = AppDomain.CurrentDomain.BaseDirectory, ExceptionType = ex.GetType().FullName, ExceptionMessage = ex.Message, ExceptionAdditionalInfo = ex.StackTrace, Identity = Thread.CurrentPrincipal.Identity.Name, LoggerName = String.Format("WebAPI.{0}.{1}", context.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName, context.ActionContext.ActionDescriptor.ActionName), LogLevel = LogRecord.ELogLevel.Error, Message = null, ProcessId = process.Id, ProcessName = process.ProcessName, Server = Environment.MachineName, ThreadId = Thread.CurrentThread.ManagedThreadId, TimeUtc = DateTime.UtcNow, AdditionalFields = new Dictionary <String, Object> { { "Url", request.RequestUri.AbsoluteUri }, { "Host", request.Headers.Host }, { "RequestData", ExtractHeaders(buffer, request.Headers) }, } }; var response = context.Response; if (response != null) { logrec.AdditionalFields.Add("HttpStatusCode", response.StatusCode); logrec.AdditionalFields.Add("ResponseData", ExtractHeaders(buffer, response.Headers)); } connector.SendLogRecord(logrec); }
protected override void Append(LoggingEvent loggingEvent) { connector.SendLogRecord(ConvertLogEventToLogRecord(loggingEvent)); }
static void Main(string[] args) { int numberOfLogs = 10000; int batchSize = 0; var p = new OptionSet { { "n|nlogs=", "number of logs to generate", v => numberOfLogs = Int32.Parse(v) }, { "b|batchsize=", "send logs in batches", v => batchSize = Int32.Parse(v) }, }; List <String> extra; try { extra = p.Parse(args); } catch (Exception ex) { Console.Write("testlog: "); Console.WriteLine(ex.Message); Console.WriteLine("Try `testlog --help' for more information."); return; } if (extra.Count == 0) { Console.WriteLine("ERROR: Missing diagnostics url."); Console.WriteLine(); p.WriteOptionDescriptions(Console.Out); return; } int errorsCnt = 0; int recordsGenerated = 0; long ticks = 0; using (new Timer((o) => { Console.Write("\rProcessed: {0} / {1} ", recordsGenerated, numberOfLogs); }, null, 0, 500)) { var sw = new Stopwatch(); sw.Start(); using (var connector = new HttpCastleConnector(new Uri(extra[0]))) { var opt = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; if (batchSize > 0) { Parallel.For(0, numberOfLogs / batchSize, opt, (i) => { var recs = new LogRecord[batchSize]; for (int j = 0; j < batchSize; j++) { recs[j] = GenerateRandomLogRecord(); } try { connector.SendLogRecords(recs); Interlocked.Add(ref ticks, sw.Elapsed.Ticks); Interlocked.Add(ref recordsGenerated, batchSize); } catch (Exception ex) { Interlocked.Increment(ref errorsCnt); Console.WriteLine("Error occured: {0} - {1}", ex.GetType().FullName, ex.Message); } }); } else { Parallel.For(0, numberOfLogs, opt, (i) => { try { connector.SendLogRecord(GenerateRandomLogRecord()); Interlocked.Increment(ref recordsGenerated); } catch (Exception ex) { Interlocked.Increment(ref errorsCnt); Console.WriteLine("Error occured: {0} - {1}", ex.GetType().FullName, ex.Message); } }); } } sw.Stop(); var ts = sw.Elapsed; Console.WriteLine("\rRecords generated: {0}, errors: {1}, time: {2:#,#.##} ms which gives {3:0.000} processed records / sec", recordsGenerated, errorsCnt, ts.TotalMilliseconds, recordsGenerated / ts.TotalSeconds); } }
protected override void Write(LogEventInfo logEvent) { connector.SendLogRecord(ConvertLogEventToLogRecord(logEvent)); }