using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using Serilog; namespace ImportFlatTool { public static class LogUtils { private enum Severity { Information, Warning, Error } public static ILogger _log; static LogUtils() { Log.Logger = new LoggerConfiguration() .ReadFrom.AppSettings() //.Destructure.With() .WriteTo.RollingFile("log-{Date}.txt") .CreateLogger(); _log = Log.Logger; } public static void LogDebug(String msg) { try { //LogEntry entry = new LogEntry(); //entry.Severity = TraceEventType.Information; //entry.Message = msg; writeWrapped(Severity.Information, msg); } catch { } } public static void LogDebug(string msg, params object[] param) { try { LogDebug(String.Format(msg, param)); } catch { } } public static void LogWarning(String msg, Exception ex) { try { //LogEntry entry = new LogEntry(); //entry.Severity = TraceEventType.Warning; //entry.Message = msg + Environment.NewLine + ex.Message; writeWrapped(Severity.Warning, msg + Environment.NewLine + ex.Message); } catch (Exception) { } } public static void LogWarning(String msg) { try { //LogEntry entry = new LogEntry(); //entry.Severity = TraceEventType.Warning; //entry.Message = msg; writeWrapped(Severity.Warning , msg); } catch { } } public static void LogError(Exception ex) { try { //LogEntry entry = new LogEntry(); //entry.Severity = TraceEventType.Error; //entry.Message = ex.Message; writeWrapped(Severity.Error, ex.Message); } catch { } } public static void LogError(string msg) { try { //LogEntry entry = new LogEntry(); //entry.Severity = TraceEventType.Error; //entry.Message = msg; writeWrapped(Severity.Error, msg); } catch { } } public static void LogError(string msg, params object[] param) { try { LogError(String.Format(msg, param)); } catch { } } private static StackFrame getCallingStackFrame() { // Determine the method that called the one that is calling this one. // This is not just two up the Stack because of RichException support. StackFrame sf = null; // Start at 2. 1 for this one and another for the one above that. StackTrace st = new StackTrace(2, true); Type thisType = typeof(LogUtils); foreach (StackFrame sfi in st.GetFrames()) { // Find a calling method that is not part of this log class but is part // of the same Namespace. Type callType = sfi.GetMethod().DeclaringType; if (callType != thisType && !callType.IsInterface) { var mb = sfi.GetMethod(); var methodName = mb != null ? mb.Name : ""; if (methodName.ToLower() == "w" || methodName.Length == 0) continue; sf = sfi; break; } } return sf; } private static String addMetadataToMessage(String msg) { StackFrame sf = getCallingStackFrame(); if (sf != null) // if found add info to log message { var mb = sf.GetMethod(); var methodName = mb != null ? mb.Name : ""; var fileName = sf.GetFileName(); if (fileName != null) fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1); var lineNumber = sf.GetFileLineNumber(); if (fileName != null) { msg = fileName + "(" + lineNumber + ") - " + methodName + " - " + msg; } else { msg = "unknown - " + methodName + " - " + msg; } } return msg; } private static void writeWrapped(Severity severity, string message) // LogEntry entry/*, bool useDebug*/) { try { /*if (IncludeMetadata) */ message = addMetadataToMessage(message); // if (useDebug) // { // if (entry.Severity == TraceEventType.Error) // { //Microsoft.Office.Server.Diagnostics.PortalLog.LogString(entry.Message); // Debug.WriteLine("================== ERROR ===================="); // } // Debug.WriteLine(entry.Message); // } // else // { switch (severity) { case Severity.Information: _log.Information(message); break; case Severity.Warning: _log.Warning(message); break; default: _log.Error(message); break; } //Logger.Write(entry); // } } catch { } } } }