using System; using System.Diagnostics; using GeneralApi.Core.Services; using Microsoft.Practices.EnterpriseLibrary.Logging; using MSLogger = Microsoft.Practices.EnterpriseLibrary.Logging.Logger; namespace GeneralApi.Core.Common { public class Logger : ILogger { public void LogDebug(String msg) { try { LogEntry entry = new LogEntry(); entry.Severity = TraceEventType.Information; entry.Message = msg; writeWrapped(entry); } catch { } } public void LogDebug(string msg, params object[] param) { try { LogDebug(String.Format(msg, param)); } catch { } } public void LogWarning(String msg, Exception ex) { try { LogEntry entry = new LogEntry(); entry.Severity = TraceEventType.Warning; entry.Message = msg + Environment.NewLine + ex.Message; writeWrapped(entry); } catch (Exception) { } } public void LogWarning(String msg) { try { LogEntry entry = new LogEntry(); entry.Severity = TraceEventType.Warning; entry.Message = msg; writeWrapped(entry); } catch { } } public void LogError(Exception ex) { try { LogEntry entry = new LogEntry(); entry.Severity = TraceEventType.Error; entry.Message = ex.Message; writeWrapped(entry); } catch { } } public void LogError(string msg) { try { LogEntry entry = new LogEntry(); entry.Severity = TraceEventType.Error; entry.Message = msg; writeWrapped(entry); } catch { } } public void LogError(string msg, params object[] param) { try { LogError(String.Format(msg, param)); } catch { } } private 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(Logger); 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 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 void writeWrapped(LogEntry entry/*, bool useDebug*/) { try { /*if (IncludeMetadata) */ entry.Message = addMetadataToMessage(entry.Message); // if (useDebug) // { // if (entry.Severity == TraceEventType.Error) // { //Microsoft.Office.Server.Diagnostics.PortalLog.LogString(entry.Message); // Debug.WriteLine("================== ERROR ===================="); // } // Debug.WriteLine(entry.Message); // } // else // { MSLogger.Write(entry); // } } catch { } } } }