private LogEntry CreateLogEntry(string message, string severity) { LogEntryAppLevelConstantInfo logEntryAppLevelConstantInfo = LogEntryAppLevelConstantInfo.GetAppConstantInfo(); Process currentProcess = Process.GetCurrentProcess(); LogEntry logEntry = new LogEntry { Id = Guid.NewGuid(), Message = message, Severity = severity, LogData = LogData, AppServerDateTime = DateTimeProvider.GetCurrentUtcDateTime(), AppServerThreadId = Environment.CurrentManagedThreadId, MemoryUsage = currentProcess.PrivateMemorySize64, AppEnvironmentName = logEntryAppLevelConstantInfo.AppEnvironmentName, ApplicationName = logEntryAppLevelConstantInfo.ApplicationName, AppServerAppDomainName = logEntryAppLevelConstantInfo.AppServerAppDomainName, AppServerName = logEntryAppLevelConstantInfo.AppServerName, AppServerOSVersion = logEntryAppLevelConstantInfo.AppServerOSVersion, AppServerProcessId = logEntryAppLevelConstantInfo.AppServerProcessId, AppServerUserAccountName = logEntryAppLevelConstantInfo.AppServerUserAccountName, AppServerWas64Bit = logEntryAppLevelConstantInfo.AppServerWas64Bit, AppVersion = logEntryAppLevelConstantInfo.AppVersion, AppWas64Bit = logEntryAppLevelConstantInfo.AppWas64Bit, AppWasInDebugMode = logEntryAppLevelConstantInfo.AppWasInDebugMode }; return(logEntry); }
public virtual void Initialize(ITelemetry telemetry) { LogEntryAppLevelConstantInfo logEntryAppLevelConstantInfo = LogEntryAppLevelConstantInfo.GetAppConstantInfo(); if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.ApplicationName))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.ApplicationName), logEntryAppLevelConstantInfo.ApplicationName); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppVersion))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppVersion), logEntryAppLevelConstantInfo.AppVersion); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppEnvironmentName))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppEnvironmentName), logEntryAppLevelConstantInfo.AppEnvironmentName); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppWasInDebugMode))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppWasInDebugMode), logEntryAppLevelConstantInfo.AppWasInDebugMode.ToString()); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerName))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerName), logEntryAppLevelConstantInfo.AppServerName); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerOSVersion))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerOSVersion), logEntryAppLevelConstantInfo.AppServerOSVersion); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerAppDomainName))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerAppDomainName), logEntryAppLevelConstantInfo.AppServerAppDomainName); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerProcessId))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerProcessId), logEntryAppLevelConstantInfo.AppServerProcessId.ToString()); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerUserAccountName))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerUserAccountName), logEntryAppLevelConstantInfo.AppServerUserAccountName); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerWas64Bit))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerWas64Bit), logEntryAppLevelConstantInfo.AppServerWas64Bit.ToString()); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppWas64Bit))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppWas64Bit), logEntryAppLevelConstantInfo.AppWas64Bit.ToString()); } if (telemetry is RequestTelemetry requestTelemetry && HttpContextAccessor.HttpContext != null) { HttpContextAccessor.HttpContext.Items[nameof(RequestTelemetry)] = requestTelemetry; } }
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { LogEntryAppLevelConstantInfo logEntryAppLevelConstantInfo = LogEntryAppLevelConstantInfo.GetAppConstantInfo(); logEvent.AddOrUpdateProperty(new LogEventProperty(nameof(LogEntry.ApplicationName), new ScalarValue(logEntryAppLevelConstantInfo.ApplicationName))); logEvent.AddOrUpdateProperty(new LogEventProperty(nameof(LogEntry.AppVersion), new ScalarValue(logEntryAppLevelConstantInfo.AppVersion))); logEvent.AddOrUpdateProperty(new LogEventProperty(nameof(LogEntry.AppEnvironmentName), new ScalarValue(logEntryAppLevelConstantInfo.AppEnvironmentName))); logEvent.AddOrUpdateProperty(new LogEventProperty(nameof(LogEntry.AppWasInDebugMode), new ScalarValue(logEntryAppLevelConstantInfo.AppWasInDebugMode))); logEvent.AddOrUpdateProperty(new LogEventProperty(nameof(LogEntry.AppServerName), new ScalarValue(logEntryAppLevelConstantInfo.AppServerName))); logEvent.AddOrUpdateProperty(new LogEventProperty(nameof(LogEntry.AppServerOSVersion), new ScalarValue(logEntryAppLevelConstantInfo.AppServerOSVersion))); logEvent.AddOrUpdateProperty(new LogEventProperty(nameof(LogEntry.AppServerAppDomainName), new ScalarValue(logEntryAppLevelConstantInfo.AppServerAppDomainName))); logEvent.AddOrUpdateProperty(new LogEventProperty(nameof(LogEntry.AppServerProcessId), new ScalarValue(logEntryAppLevelConstantInfo.AppServerProcessId))); logEvent.AddOrUpdateProperty(new LogEventProperty(nameof(LogEntry.AppServerUserAccountName), new ScalarValue(logEntryAppLevelConstantInfo.AppServerUserAccountName))); logEvent.AddOrUpdateProperty(new LogEventProperty(nameof(LogEntry.AppServerWas64Bit), new ScalarValue(logEntryAppLevelConstantInfo.AppServerWas64Bit))); logEvent.AddOrUpdateProperty(new LogEventProperty(nameof(LogEntry.AppWas64Bit), new ScalarValue(logEntryAppLevelConstantInfo.AppWas64Bit))); }
public virtual void Initialize(ITelemetry telemetry) { if (telemetry == null) { throw new ArgumentNullException(nameof(telemetry)); } LogEntryAppLevelConstantInfo logEntryAppLevelConstantInfo = LogEntryAppLevelConstantInfo.GetAppConstantInfo(); if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.ApplicationName))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.ApplicationName), logEntryAppLevelConstantInfo.ApplicationName); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppVersion))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppVersion), logEntryAppLevelConstantInfo.AppVersion); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppEnvironmentName))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppEnvironmentName), logEntryAppLevelConstantInfo.AppEnvironmentName); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppWasInDebugMode))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppWasInDebugMode), logEntryAppLevelConstantInfo.AppWasInDebugMode.ToString()); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerName))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerName), logEntryAppLevelConstantInfo.AppServerName); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerOSVersion))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerOSVersion), logEntryAppLevelConstantInfo.AppServerOSVersion); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerAppDomainName))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerAppDomainName), logEntryAppLevelConstantInfo.AppServerAppDomainName); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerProcessId))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerProcessId), logEntryAppLevelConstantInfo.AppServerProcessId.ToString()); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerUserAccountName))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerUserAccountName), logEntryAppLevelConstantInfo.AppServerUserAccountName); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppServerWas64Bit))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppServerWas64Bit), logEntryAppLevelConstantInfo.AppServerWas64Bit.ToString(CultureInfo.InvariantCulture)); } if (!telemetry.Context.GlobalProperties.ContainsKey(nameof(LogEntry.AppWas64Bit))) { telemetry.Context.GlobalProperties.Add(nameof(LogEntry.AppWas64Bit), logEntryAppLevelConstantInfo.AppWas64Bit.ToString(CultureInfo.InvariantCulture)); } if (telemetry is RequestTelemetry requestTelemetry && HttpContextAccessor.HttpContext != null) { List <AppInsightsLogKeyVal> logKeyValues; if (HttpContextAccessor.HttpContext.Items.TryGetValue("LogKeyValues", out object?logKeyValuesAsObj)) { logKeyValues = (List <AppInsightsLogKeyVal>)logKeyValuesAsObj; } else // in ok responses, we've no LogKeyValues because ApplicationInsightsLogStore won't gets called. { using IDependencyResolver childResolver = DependencyManager.CreateChildDependencyResolver(); // HttpContextAccessor.HttpContext.RequestServices is null because scope is gets disposed at this time. IRequestInformationProvider requestInformationProvider = childResolver.Resolve <IRequestInformationProvider>(); IUserInformationProvider userInformationProvider = childResolver.Resolve <IUserInformationProvider>(); logKeyValues = new List <AppInsightsLogKeyVal> { }; if (userInformationProvider.IsAuthenticated()) { logKeyValues.Add(new AppInsightsLogKeyVal { Key = "UserId", Value = userInformationProvider.GetCurrentUserId() }); } logKeyValues.Add(new AppInsightsLogKeyVal { Key = nameof(IRequestInformationProvider.UserAgent), Value = requestInformationProvider.UserAgent }); logKeyValues.Add(new AppInsightsLogKeyVal { Key = "X-Correlation-ID", Value = requestInformationProvider.XCorrelationId }); } AppInsightsLogKeyVal userAgent = logKeyValues.FirstOrDefault(ld => ld.Key == nameof(IRequestInformationProvider.UserAgent)); if (userAgent != null) { requestTelemetry.Context.User.UserAgent = userAgent.Value; } AppInsightsLogKeyVal userId = logKeyValues.FirstOrDefault(ld => ld.Key == "UserId"); if (userId != null) { requestTelemetry.Context.User.AccountId = requestTelemetry.Context.User.Id = requestTelemetry.Context.User.AuthenticatedUserId = requestTelemetry.Context.User.AuthenticatedUserId = userId.Value; } foreach (AppInsightsLogKeyVal keyVal in logKeyValues.OrderBy(kv => kv.Key)) { if (keyVal.Key == nameof(IRequestInformationProvider.UserAgent) || keyVal.Key == "UserId") { continue; } if (!requestTelemetry.Properties.ContainsKey(keyVal.Key)) { requestTelemetry.Properties.Add(keyVal.Key, keyVal.Value); } } AppInsightsLogKeyVal xCorrelationId = logKeyValues.FirstOrDefault(ld => ld.Key == "X-Correlation-ID"); if (xCorrelationId != null) { requestTelemetry.Id = xCorrelationId.Value; } } }