Beispiel #1
0
        private static void handleLogging(ErrorLevel errorlevel, string message, object data, string memberName, string filePath)
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                string timestamp = DateTime.UtcNow.ToString("s", System.Globalization.CultureInfo.InvariantCulture);
                var shortPath    = Redaction.RedactEnvironmentVariables(Path.GetFileNameWithoutExtension(filePath));
                var method       = Redaction.RedactEnvironmentVariables(memberName);
                message          = $"{shortPath}:{method} {Redaction.RedactEnvironmentVariables(message)}";
                var preppedData  = FilterAndRedactData(data);

                switch (errorlevel)
                {
                case ErrorLevel.Debug:
                    {
                        if (Verbose)
                        {
                            log(timestamp, errorlevel, message, preppedData);
                        }
                        if (TelemetryEnabled)
                        {
                            RecordTelemetryInfo(errorlevel, message, preppedData);
                        }
                    }
                    break;

                case ErrorLevel.Info:
                case ErrorLevel.Warning:
                    {
                        log(timestamp, errorlevel, message, preppedData);
                        if (TelemetryEnabled)
                        {
                            RecordTelemetryInfo(errorlevel, message, preppedData);
                        }
                    }
                    break;

                case ErrorLevel.Error:
                case ErrorLevel.Critical:
                    {
                        log(timestamp, errorlevel, message, preppedData);
                        if (TelemetryEnabled)
                        {
                            ReportTelemetryEvent(timestamp, errorlevel, message, preppedData);
                        }
                    }
                    break;
                }
            }).ConfigureAwait(false);
        }
Beispiel #2
0
 private static void log(string timestamp, ErrorLevel errorlevel, string message, object data = null)
 {
     lock (logLock)
     {
         try
         {
             using (StreamWriter file = new StreamWriter(LogFile, true))
             {
                 file.WriteLine($"{timestamp} [{errorlevel}] {message}" + (data != null
                     ? $": {Redaction.RedactEnvironmentVariables(JsonConvert.SerializeObject(data))}"
                     : null));
             }
         }
         catch (Exception)
         {
             // Failed; can't do anything about it as we're in the logging code anyway
         }
     }
 }
Beispiel #3
0
 private static Dictionary <string, object> FilterAndRedactData(object data)
 {
     if (data is null)
     {
         return(null);
     }
     try
     {
         if (data is string str && !JsonRegex.IsMatch(str))
         {
             return(Wrap("message", Redaction.RedactEnvironmentVariables(str)));
         }
         else
         {
             // Serialize the data to a string
             string serialized = JsonConvert.SerializeObject(data);
             serialized = FilterPropertiesFromJsonString(serialized);
             serialized = Redaction.RedactEnvironmentVariables(serialized);
             if (data is Exception)
             {
                 return(JsonConvert.DeserializeObject <Dictionary <string, object> >(serialized));
             }
             else
             {
                 var jToken = JToken.Parse(serialized);
                 if (jToken is JArray jArray)
                 {
                     return(Wrap("data", jArray));
                 }
                 if (jToken is JObject jObject)
                 {
                     return(jObject.ToObject <Dictionary <string, object> >());
                 }
             }
         }
     }