public void WriteToLogLevel(string message, LogLevelEnum level, Exception ex) { int depth = 1; StackFrame stackframe = new StackFrame(depth + 1); string _line = "0"; string _te = ex.StackTrace.ToString(); int _lis = _te.IndexOf("line"); if (_lis >= 0) { _line = _te.Substring(_lis, 7);//stackframe.GetFileLineNumber() } string msgcname = message + " -CLASS :" + stackframe.GetMethod().DeclaringType.FullName + " -METHOD :" + stackframe.GetMethod().Name + "()" + " -LINE :" + _line; if (loglevel.ToString() == (level.ToString()) || loglevel.ToString().Equals("All")) { switch (level)// writes 'level' category of log { case LogLevelEnum.Debug: Debug(msgcname, ex); break; case LogLevelEnum.Info: Info(msgcname, ex); break; case LogLevelEnum.Warn: Warn(msgcname, ex); break; case LogLevelEnum.Error: Error(msgcname, ex); break; case LogLevelEnum.Fatal: Fatal(msgcname, ex); break; default: Error(msgcname, ex); break; } } else if (level.ToString().Trim().Equals("Error") || level.ToString().Trim().Equals("Fatal"))//if invalid log level in config, still Error level logging will be done if level is "error"/"fatal" { Error(" Off or Invalid Log level set in Config file. Valid options(case sensitive) are Debug, Info, Warn, Error, Fatal, All, Off.\n " + msgcname + "\n", ex); } }
public void Log(LogLevelEnum level, string message, params object[] additionalData) { if (level >= _minimumLogLevel) { string source = _source?.FullyQualifiedName ?? "default"; Console.WriteLine($"{source} - {level.ToString().ToUpper()} : {string.Format(message, additionalData)}"); } }
public Task LogAsync(LogLevelEnum level, string message, params object[] additionalData) { if (level >= _minimumLogLevel) { string source = _source?.FullyQualifiedName ?? "default"; Write(level, $"{source} - {level.ToString().ToUpper()} : {string.Format(message, additionalData)}"); } return Task.FromResult(0); }
public void WriteToLogLevel(string message, LogLevelEnum level) { int depth = 1; StackFrame stackframe = new StackFrame(depth); //24Nov2015 While working with SQL found that stackframe is null. Reason unknown. But this caused app crash //so here is some code that may stop that crash even if the stackframe is null string classname = " -CLASS :Unknonwn-Class"; string methodname = " -METHOD : Unknown-Method"; if (stackframe.GetMethod() != null) { classname = " -CLASS :" + stackframe.GetMethod().DeclaringType.FullName; methodname = " -METHOD :" + stackframe.GetMethod().Name + "()"; } //StackTrace stacktrace = new StackTrace(depth); //string _line=string.Empty; //string _te = stacktrace.ToString(); //int _lis = _te.IndexOf("line"); //if(_lis>0) //_line = _te.Substring(_lis, 7);//stackframe.GetFileLineNumber() string msgcname = message + classname + methodname; // + //" -LINE :" + _line; if (loglevel.ToString() == (level.ToString()) || loglevel.ToString().Equals("All")) { switch (level) // writes 'level' category of log { case LogLevelEnum.Debug: Debug(msgcname); break; case LogLevelEnum.Info: Info(msgcname); break; case LogLevelEnum.Warn: Warn(msgcname); break; case LogLevelEnum.Error: Error(msgcname); break; case LogLevelEnum.Fatal: Fatal(msgcname); break; default: Error(msgcname); break; } } //else //if invalid log level in config, still Error level logging will be done // Error("Invalid Log level set in Config file. Valid options(case sensitive) are Debug, Info, Warn, Error, Fatal, All\n"+msgcname); }
private static string EnrichLogMessage(LogLevelEnum level, string message, string memberName, string sourceFilePath) { var sb = new StringBuilder(); var d = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.ffff"); var prefix = string.Format("[{0}] \t [{1}]", d, level.ToString()); sb.AppendLine(string.Format("{0} \t [MemberName] {1}", prefix, memberName)); sb.AppendLine(string.Format("{0} \t [FileName] {1}", prefix, sourceFilePath)); sb.AppendLine(string.Format("{0} \t [Message] {1}", prefix, message)); return(sb.ToString()); }
public static LogContent CreateLog(LogLevelEnum level, string logProvider, string title, string content, IDictionary <string, Tag> tags) { var logContent = new LogContent() { Content = content, Level = level.ToString(), LogTimeStamp = DateTime.UtcNow.ToString("o"), Provider = logProvider, Tags = tags, Title = title }; return(logContent); }
public void Log(MessageStructure message, LogLevelEnum level = LogLevelEnum.Informative) { using (DatabaseContext context = new DatabaseContext()) { context.Log.Add(new LogModel() { Message = message.Message, FileName = message.FileName, Line = message.LineNumber, OriginName = message.OriginName, LogCategory = level.ToString(), Time = DateTime.Now }); context.SaveChanges(); } }
public Task Log(LogLevelEnum level, string message, Exception exception) { if (level < _minimumLogLevel) return Task.FromResult(0); Dictionary<string, string> properties = new Dictionary<string, string> { {"component", _fullyQualifiedName.ToString()}, {"level", level.ToString()}, {"message", message} }; if (exception != null) { _telemetryClient.TrackException(exception, properties); } _telemetryClient.TrackEvent($"LOG:{level}", properties); return Task.FromResult(0); }
public void WriteToLogLevel(string message, LogLevelEnum level) { int depth = 1; StackFrame stackframe = new StackFrame(depth); //StackTrace stacktrace = new StackTrace(depth); //string _line=string.Empty; //string _te = stacktrace.ToString(); //int _lis = _te.IndexOf("line"); //if(_lis>0) //_line = _te.Substring(_lis, 7);//stackframe.GetFileLineNumber() string msgcname = message + " -CLASS :" + stackframe.GetMethod().DeclaringType.FullName + " -METHOD :" + stackframe.GetMethod().Name + "()";// + //" -LINE :" + _line; if (loglevel.ToString() == (level.ToString()) || loglevel.ToString().Equals("All")) { switch (level) // writes 'level' category of log { case LogLevelEnum.Debug: Debug(msgcname); break; case LogLevelEnum.Info: Info(msgcname); break; case LogLevelEnum.Warn: Warn(msgcname); break; case LogLevelEnum.Error: Error(msgcname); break; case LogLevelEnum.Fatal: Fatal(msgcname); break; default: Error(msgcname); break; } } //else //if invalid log level in config, still Error level logging will be done // Error("Invalid Log level set in Config file. Valid options(case sensitive) are Debug, Info, Warn, Error, Fatal, All\n"+msgcname); }
public void Log(LogLevelEnum level, string message, params object[] args) { //By firing the messagebox in a task, we don't block the ui of the main application. //In some instances this isn't the best way to go, as we would want the user to have to interact //with the messagebox first. //Also, the messageboxes seem to be waiting on each other, so you only get the second after closing the first // and so on. Task.Factory.StartNew ( () => { System.Windows.MessageBox.Show( Utils.Format(message, args), level.ToString(), System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Information, System.Windows.MessageBoxResult.OK, System.Windows.MessageBoxOptions.DefaultDesktopOnly); } ); }
public static void SetLogLevel(LogLevelEnum logLevelEnum) { LogLevel = logLevelEnum; Log("LoggerFactory", LogLevelEnum.Debug, "Set the level to " + LogLevel.ToString(), null); }
public LogLevel(LogLevelEnum logLevel) : this(logLevel.ToString(), (int)logLevel) { }
public Task LogAsync(LogLevelEnum level, string message, Exception exception, params object[] additionalData) { if (level >= _minimumLogLevel) { string source = _source?.FullyQualifiedName ?? "default"; Console.WriteLine($"{source} - {level.ToString().ToUpper()} : {exception.GetType().Name} - {string.Format(message,additionalData)}"); } return Task.FromResult(0); }
public void Log(LogLevelEnum level, String message, params Object[] args) { Console.WriteLine(String.Format("[{0}] {1} ", level.ToString(), message), args); }
public void Log(LogLevelEnum level, Exception ex, String message, params Object[] args) { Console.WriteLine(String.Format("[{0}] {1} ", level.ToString(), ex.ToString() + Environment.NewLine + message), args); }
/// <summary> /// Log a message at the specified level /// </summary> /// <param name="level">The level to log at</param> /// <param name="message">The message to log</param> /// <param name="exception">An exception to log</param> /// <param name="additionalData">Optional additional data to supply to the logger</param> public void Log(LogLevelEnum level, string message, Exception exception, params object[] additionalData) { if (level >= _minimumLogLevel) { string source = _source?.FullyQualifiedName ?? "default"; Write(level, $"{source} - {level.ToString().ToUpper()} : {exception.GetType().Name} - {string.Format(message, additionalData)}"); } }
//************************************************************************ /// <summary> /// LOG文件输出 /// <param name="argLevel">错误级别</param> /// <param name="argFqcn">Class名</param> /// <param name="argMethodName">method名</param> /// <param name="argMessage">msg</param> /// <param name="argEx">异常类(有异常时)</param> /// <param name="argUserID">用户名</param> /// <param name="argIpAddress">IPAddress</param> /// </summary> //************************************************************************ private void WriteLogFile(LogLevelEnum argLevel, string argFqcn, string argMethodName, string argMessage, Exception argEx) { if (((int)argLevel) < m_logLevel) { return; } if ((m_logFlushIntervalMSecs > 0) && (!isActive)) { writeThread = new Thread(Flush); writeThread.IsBackground = true; writeThread.Start(); sb = new StringBuilder(); isActive = true; } if (m_logFlushIntervalMSecs > 0 && ((int)argLevel) == 1 && sb != null && sb.Length < int.MaxValue) { #region 第二种模式 lock (sb) { try { sb.AppendFormat("{0}\t{1}\t{2}\r\n", DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss.FFF"), argMethodName, argMessage); if (argEx != null) { sb.AppendLine(argEx.ToString()); } } catch { } } #endregion } else { #region 第一种模式 try { CMLogger.mu.WaitOne(); //if (String.IsNullOrEmpty(argIpAddress)) //{ // argIpAddress = CMUtility.GetUserIp(); //} string strSysTime = DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss.FFF").Trim(); string processName = Process.GetCurrentProcess().Id.ToString(); string logFullPath = m_logFilePath + "\\" + argLevel.ToString() + "\\" + strSysTime.Substring(0, 13) + processName + ".log"; CreateDirectory(m_logFilePath); //写字符串构成 //string logstr = strSysTime + "\t" // //+ (String.IsNullOrEmpty(argIpAddress) ? "\t" : CMUtility.GetUserIp() + "\t") // + (argFqcn + "(" + argMethodName + ")\t") // + (String.IsNullOrEmpty(argMessage) ? "\t" : argMessage + "\t") // + (argEx == null ? "\t" : argEx.Message + argEx.StackTrace + "\t"); if (!File.Exists(logFullPath)) { // Create a file to write to. using (StreamWriter sw = File.CreateText(logFullPath)) { sw.WriteLine("{0}\t{1}\t{2}", DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss.FFF"), argMethodName, argMessage); if (argEx != null) { sw.WriteLine(argEx.ToString()); } sw.Close(); } } else { using (StreamWriter sw = File.AppendText(logFullPath)) { sw.WriteLine("{0}\t{1}\t{2}", DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss.FFF"), argMethodName, argMessage); if (argEx != null) { sw.WriteLine(argEx.ToString()); } sw.Close(); } } } catch { //throw ex; } finally { CMLogger.mu.ReleaseMutex(); } #endregion } }
public void Write(LogLevelEnum level, string message) { Trace.WriteLine(message, level.ToString()); }
/// <summary> /// 組成Log訊息 /// </summary> /// <param name="logLevel">Log層級</param> /// <param name="msg">原始Log訊息</param> /// <returns>加上Log其它資訊的完整Log訊息</returns> protected string MakeLogMessage(LogLevelEnum logLevel, string msg) { return(string.Format("{0} [{1}] {2}", DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff"), logLevel.ToString(), msg)); }
/// <summary> /// 組成Log訊息 /// </summary> /// <param name="logLevel">Log層級</param> /// <param name="msg">原始Log訊息</param> /// <returns>加上Log其它資訊的完整Log訊息</returns> protected string MakeLogMessage(LogLevelEnum logLevel, string msg) { return string.Format("{0} [{1}] {2}", DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff"), logLevel.ToString(), msg); }
public Task Log(LogLevelEnum level, string message) { Console.WriteLine("{0}: {1}", level.ToString().ToUpper(), message); return Task.FromResult(0); }
public Task Log(LogLevelEnum level, string message, Exception exception) { Console.WriteLine("{0} {1}: {2}", level.ToString().ToUpper(), exception.GetType().Name, message); return Task.FromResult(0); }