/// <summary> /// If addDateTime is sppecified Year, Month, Day properties are added. If time portion is not 00:00:00.000 then also Hour is added. /// </summary> public void LogEvent(TelemetryEvent eventName, ITelemetryPropertyProvider properties, DateTime?addDateTime) { if (AppConfig.Logging.LogEvents) { LogEvent(eventName, GetProperties(LogType.Event, properties, addDateTime, null)); // don't duplicate EventName property } }
/// <summary> /// If addDateTime is sppecified Year, Month, Day properties are added. If time portion is not 00:00:00.000 then also Hour is added. /// </summary> public void LogMetrics(TelemetryMetric[] metrics, double value, ITelemetryPropertyProvider properties, DateTime?addDateTime) { if (AppConfig.Logging.LogMetrics) { LogMetrics(metrics, value, GetProperties(LogType.Metrics, properties, addDateTime, null)); } }
/// <summary> /// If addDateTime is sppecified Year, Month, Day properties are added. If time portion is not 00:00:00.000 then also Hour is added. /// </summary> public void LogTrace(string message, LogLevel logLevel, ITelemetryPropertyProvider properties, DateTime?addDateTime) { if (logLevel != LogLevel.None && AppConfig.Logging.LogTrace) { Client.TrackTrace(message, LogLevelToSeverityLevel[(int)logLevel], GetProperties(LogType.Trace, properties, addDateTime, null)); #if DEBUG Client.Flush(); #endif } }
///// <summary> ///// Not used ///// Errors are logged using Microsoft.Extensions.ILogger into Application Insights, see Toptal.BikeRentals.Logging ///// </summary> //public void TrackException(Exception exception) //{ // if (exception != null) // { // if (exception.InnerException != null) // TrackException(exception.InnerException); // if (exception is AppException appException) // { // Client.TrackException(exception, new Dictionary<string, string> // { // { "StatusCode", appException.Status.ToString() }, // { "StatusText", appException.StatusText.ToString() }, // { "LogLevel", appException.LogLevel.ToString() }, // { "DetailerErrorMessage ", appException.DetailerErrorMessage } // }); // } // else // Client.TrackException(exception); // } //} #endregion #region Helpers /// <summary> /// If addDateTime is sppecified Year, Month, Day properties are added. If time portion is not 00:00:00.000 then also Hour is added. /// </summary> public static Dictionary <string, string> GetProperties(LogType logType, ITelemetryPropertyProvider properties, DateTime?addDateTime, TelemetryEvent?eventName) { var dict = new Dictionary <string, string>(); if (eventName.HasValue) { dict.Add("Event name", eventName.Value.Title()); } if (addDateTime.HasValue) { dict.Add("Year", addDateTime.Value.ToString("yyyy")); dict.Add("Quarter", $"Q{(addDateTime.Value.Month + 2) / 3}"); dict.Add("Month", addDateTime.Value.ToString("MM")); dict.Add("Month name", addDateTime.Value.ToString("MM-MMMM", CultureInfo.InvariantCulture)); dict.Add("Day", addDateTime.Value.ToString("dd")); dict.Add("Event date", addDateTime.Value.Date.ToString("d", CultureInfo.InvariantCulture)); dict.Add("Event time", addDateTime.Value.ToString("g", CultureInfo.InvariantCulture)); if (addDateTime.Value.Hour != 0 || addDateTime.Value.Minute != 0 || addDateTime.Value.Second != 0 || addDateTime.Value.Millisecond != 0) { dict.Add("Hour", addDateTime.Value.ToString("hh")); } } properties?.GetTelemetryProperties(logType, string.Empty, dict); return(dict .Where(t => !string.IsNullOrEmpty(t.Value)) .ToDictionary( t => { var val = t.Key.Trim(); return string.IsNullOrEmpty(val) ? val : $"{Char.ToUpper(val[0])}{val.Substring(1).ToLower()}"; }, t => t.Value.Trim() )); }
/// <summary> /// If addDateTime is sppecified Year, Month, Day properties are added. If time portion is not 00:00:00.000 then also Hour is added. /// </summary> public void LogEventWithMetrics(TelemetryEvent eventName, TelemetryMetric[] metrics, double value, ITelemetryPropertyProvider properties, DateTime?addDateTime) { LogEvent(eventName, GetProperties(LogType.Event, properties, addDateTime, null)); // don't duplicate EventName property LogMetrics(metrics, value, GetProperties(LogType.Metrics, properties, addDateTime, eventName)); }
/// <summary> /// If addDateTime is sppecified Year, Month, Day properties are added. If time portion is not 00:00:00.000 then also Hour is added. /// </summary> public void LogMetric(TelemetryMetric metric, double value, ITelemetryPropertyProvider properties, DateTime?addDateTime) { LogMetrics(new[] { metric }, value, properties, addDateTime); }