|
1 | | -using System; |
| 1 | +using cAlgo.API.Alert.Models; |
| 2 | +using System; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
2 | 7 |
|
3 | 8 | namespace cAlgo.API.Alert.Utility |
4 | 9 | { |
5 | | - public class Logger |
| 10 | + public static class Logger |
6 | 11 | { |
7 | | - #region Fields |
| 12 | + #region Methods |
8 | 13 |
|
9 | | - private Action<string> _tracer; |
| 14 | + public static void LogException(Exception exception) |
| 15 | + { |
| 16 | + string exceptionData = GetExceptionData(exception); |
10 | 17 |
|
11 | | - #endregion Fields |
| 18 | + Log(exceptionData); |
| 19 | + } |
12 | 20 |
|
13 | | - public Logger(Action<string> tracer) |
| 21 | + public static void Log(string message, params object[] parameters) |
14 | 22 | { |
15 | | - _tracer = tracer; |
| 23 | + string messageFormatted = string.Format(message, parameters); |
16 | 24 |
|
17 | | - AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; |
18 | | - } |
| 25 | + Configuration.Current.Tracer?.Invoke(messageFormatted); |
19 | 26 |
|
20 | | - #region Methods |
| 27 | + string logFilePath = Configuration.Current.LogFilePath; |
21 | 28 |
|
22 | | - public void Log(string message) |
23 | | - { |
24 | | - _tracer.Invoke(message); |
| 29 | + using (FileStream stream = File.Open(logFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)) |
| 30 | + using (TextWriter writer = new StreamWriter(stream)) |
| 31 | + { |
| 32 | + string line = string.Format("{0:yyyy-MM-dd HH:mm:ss} => {1}", DateTime.UtcNow, messageFormatted); |
| 33 | + |
| 34 | + writer.WriteLine(line); |
| 35 | + } |
25 | 36 | } |
26 | 37 |
|
27 | | - public void Log(Exception exception) |
| 38 | + private static string GetExceptionData(Exception exception, bool addStackTrace = true, bool isInnerException = false, |
| 39 | + string intend = null) |
28 | 40 | { |
29 | | - string exceptionSummary = ExceptionTools.GetDetail(exception); |
| 41 | + StringBuilder stringBuilder = new StringBuilder(); |
| 42 | + |
| 43 | + intend = intend ?? string.Empty; |
| 44 | + |
| 45 | + if (isInnerException) |
| 46 | + { |
| 47 | + stringBuilder.AppendLine(); |
| 48 | + stringBuilder.Append(string.Format("{0}InnerException:", intend)); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + string systemType = Environment.Is64BitOperatingSystem ? "64-bit" : "32-bit"; |
| 53 | + |
| 54 | + stringBuilder.AppendLine(); |
| 55 | + stringBuilder.Append(string.Format("OS Version: {0}", Environment.OSVersion.VersionString)); |
| 56 | + stringBuilder.AppendLine(); |
| 57 | + stringBuilder.Append(string.Format("System Type: {0}", systemType)); |
| 58 | + } |
| 59 | + |
| 60 | + stringBuilder.AppendLine(); |
| 61 | + stringBuilder.Append(string.Format("{0}Source: {1}", intend, exception.Source)); |
| 62 | + stringBuilder.AppendLine(); |
| 63 | + stringBuilder.Append(string.Format("{0}Message: {1}", intend, exception.Message)); |
| 64 | + stringBuilder.AppendLine(); |
| 65 | + stringBuilder.Append(string.Format("{0}TargetSite: {1}", intend, exception.TargetSite)); |
| 66 | + stringBuilder.AppendLine(); |
| 67 | + stringBuilder.Append(string.Format("{0}Type: {1}", intend, exception.GetType())); |
| 68 | + stringBuilder.AppendLine(); |
| 69 | + |
| 70 | + if (addStackTrace) |
| 71 | + { |
| 72 | + string stackTrace = GetExceptionStackTrace(exception, intend); |
| 73 | + |
| 74 | + stringBuilder.AppendLine(stackTrace); |
| 75 | + |
| 76 | + stringBuilder.AppendLine(); |
| 77 | + } |
30 | 78 |
|
31 | | - _tracer.Invoke(exceptionSummary); |
| 79 | + if (exception.InnerException != null) |
| 80 | + { |
| 81 | + string innerExceptionIntent = new string(' ', intend.Length + 4); |
| 82 | + |
| 83 | + string innerExceptionSummary = GetExceptionData(exception.InnerException, isInnerException: true, intend: innerExceptionIntent); |
| 84 | + |
| 85 | + stringBuilder.Append(innerExceptionSummary); |
| 86 | + } |
| 87 | + |
| 88 | + return stringBuilder.ToString(); |
32 | 89 | } |
33 | 90 |
|
34 | | - private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) |
| 91 | + private static string GetExceptionStackTrace(Exception exception, string intend = null) |
35 | 92 | { |
36 | | - if (e.ExceptionObject != null) |
| 93 | + if (string.IsNullOrEmpty(exception.StackTrace)) |
37 | 94 | { |
38 | | - Log(e.ExceptionObject as Exception); |
| 95 | + return string.Empty; |
39 | 96 | } |
| 97 | + |
| 98 | + StackTrace stackTrace = new StackTrace(exception, true); |
| 99 | + |
| 100 | + StackFrame[] frames = stackTrace.GetFrames(); |
| 101 | + |
| 102 | + if (frames == null || !frames.Any()) |
| 103 | + { |
| 104 | + return string.Empty; |
| 105 | + } |
| 106 | + |
| 107 | + StringBuilder stringBuilder = new StringBuilder(); |
| 108 | + |
| 109 | + stringBuilder.Append(string.Format("{0}StackTrace:", intend)); |
| 110 | + stringBuilder.AppendLine(); |
| 111 | + |
| 112 | + string tracesIntend = new string(' ', string.IsNullOrEmpty(intend) ? 4 : intend.Length + 4); |
| 113 | + |
| 114 | + foreach (StackFrame stackFram in frames) |
| 115 | + { |
| 116 | + string fileName = stackFram.GetFileName(); |
| 117 | + |
| 118 | + fileName = !string.IsNullOrEmpty(fileName) |
| 119 | + ? fileName.Substring(fileName.LastIndexOf(@"\", StringComparison.InvariantCultureIgnoreCase) + 1) |
| 120 | + : string.Empty; |
| 121 | + |
| 122 | + string stackFrameText = string.Format("{0}File: {1} | Line: {2} | Col: {3} | Offset: {4} | Method: {5}", tracesIntend, fileName, |
| 123 | + stackFram.GetFileLineNumber(), stackFram.GetFileColumnNumber(), stackFram.GetILOffset(), stackFram.GetMethod()); |
| 124 | + |
| 125 | + stringBuilder.Append(stackFrameText); |
| 126 | + |
| 127 | + stringBuilder.AppendLine(); |
| 128 | + } |
| 129 | + |
| 130 | + return stringBuilder.ToString(); |
40 | 131 | } |
41 | 132 |
|
42 | 133 | #endregion Methods |
|
0 commit comments