public bool CheckConnection(string connectionName = null) { if (connectionName == null) { connectionName = GetRandomDbName(); } int v = 0; try { using (var db = _dbFactory.Open(connectionName)) { //db.ExecuteSql($"INSERT INTO test (firstname) values ({DateTime.Now.Millisecond})"); //return true; v = db.Single <int>("SELECT 1"); if (v == 1) { OnLogEvent?.Invoke(this, $"{connectionName} connected"); } else { OnLogEvent?.Invoke(this, $"{connectionName} NOT connected"); } } } catch (MySqlException e) { OnLogEvent?.Invoke(this, $"{nameof(MySqlException)}: {e.Message}"); return(false); } return(v == 1); }
private async Task InvokeLogEvent(string message) { if (OnLogEvent != null) { await OnLogEvent.Invoke(message); } }
/// <summary> /// This method logs the event. /// </summary> /// <param name="logEvent">The event to log.</param> public Task Log(LogEvent logEvent) { mQueue.Enqueue(logEvent); Interlocked.Increment(ref mLogEvents); try { OnLogEvent?.Invoke(Level, logEvent); } catch (Exception) { } while (mQueue.Count > mCapacity) { LogEvent oldEvent; if (!mQueue.TryDequeue(out oldEvent)) { break; } Interlocked.Increment(ref mLogEventsExpired); } return(Task.CompletedTask); }
public T ValidateObject <T>(String filename) { Errors = false; XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema; settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); try { XmlReader reader = XmlReader.Create(filename, settings); XmlSerializer manifestMaker = new XmlSerializer(typeof(T)); T obj = (T)manifestMaker.Deserialize(reader); reader.Close(); return(obj); } catch (Exception exc) { if (OnLogEvent != null) { OnLogEvent.Invoke("Error validating object: " + exc.Message); } return(default(T)); } }
public static void Write(string text, ConsoleColor color = ConsoleColor.White) { MainLog.Add(text); foreach (var log in AlternativeLogs) { log.Value.WriteLine(text); } OnLogEvent?.Invoke(text, color); }
protected override void Append(LoggingEvent loggingEvent) { var writer = new StringWriter(new StringBuilder()); this.Layout?.Format(writer, loggingEvent); if (string.IsNullOrEmpty(writer.ToString())) { writer.WriteLine(loggingEvent.RenderedMessage); } OnLogEvent?.Invoke(this, writer.ToString()); }
private void SetTableMeta() { // We get the current assembly through the current class var currentAssembly = Assembly.GetExecutingAssembly(); // we filter the defined classes according to the interfaces they implement var stuff = currentAssembly.DefinedTypes.Where(type => type.ImplementedInterfaces.Any(inter => inter == typeof(IOrmLiteTableMetaData))).ToList(); foreach (Type type in stuff) { IOrmLiteTableMetaData temp = (IOrmLiteTableMetaData)Activator.CreateInstance(type); temp.SetTableMetaData(_dbFactory); } OnLogEvent?.Invoke(this, $"{stuff.Count} table meta data initialized"); }
// Display any warnings or errors. private void ValidationCallBack(object sender, ValidationEventArgs args) { if (args.Severity == XmlSeverityType.Warning) { if (OnLogEvent != null) { OnLogEvent.Invoke(" Validation warning: " + args.Message); } } else { if (OnLogEvent != null) { OnLogEvent.Invoke(" Validation error: " + args.Message); } Errors = true; } }
private void SetTableMeta2() { // We get the current assembly through the current class var currentAssembly = Assembly.GetExecutingAssembly(); // we filter the defined classes according to the interfaces they implement var stuff = currentAssembly.DefinedTypes.Where(type => type.IsSubclassOf(typeof(MetaDataBaseClass))).ToList(); foreach (Type type in stuff) { IOrmLiteTableMetaData temp = (IOrmLiteTableMetaData)Activator.CreateInstance(type); temp.SetTableMetaData(_dbFactory); } stuff = currentAssembly.DefinedTypes.Where(type => type.IsSubclassOf(typeof(CoreObject))).ToList(); foreach (Type type in stuff) { type.AddAttributes(new AliasAttribute("TEST_" + type.Name)); } OnLogEvent?.Invoke(this, $"{stuff.Count} table meta data initialized"); }
private void Log(string logtext) { OnLogEvent?.Invoke(logtext); }
protected override void Append(LoggingEvent loggingEvent) { var message = RenderLoggingEvent(loggingEvent); OnLogEvent?.Invoke(loggingEvent.Level, message); }
public static void InvokeLog(string message) { OnLogEvent?.Invoke(message); }
public void Write(string message) { OnLogEvent?.Invoke(message); }
private void OnLogHandle(int level, string message) { OnLogEvent?.Invoke(level, message); }
/// <summary> /// Saves a new log entry. /// </summary> /// <param name="category">The category of the entry.</param> /// <param name="message">The text to be displayed.</param> /// <param name="exception">An optional Exception object.</param> public static void AddLogEntry(LogEntryCategories category, string message, Exception exception = null, string className = "", [CallerMemberName] string memberName = "", [CallerLineNumber] int sourceLineNumber = 0) { if (!WriteLogFile) { return; } #region LogLevel check if (LogLevel == LogLevels.None) { return; } if (category == LogEntryCategories.Debug || category == LogEntryCategories.Info || category == LogEntryCategories.Other) { if (LogLevel == LogLevels.ErrorsOnly) { return; } } #endregion if (message != null) { message = message.Replace(';', ',').Replace(Environment.NewLine, " |-> ").Replace("\"", "'"); //make sure that the message does not contain any line breaks (the current CSV reader does not support multiple multi-line columns) } message = (string.IsNullOrEmpty(className) ? null : className + ".") + memberName + "[" + sourceLineNumber + "]: " + message; if (exception != null && exception.Message != null) { exception = new Exception(exception.ToString().Replace(';', ',').Replace("\"", "'")); //prevent semicolons and apostrophe in exception messages because the exceptions are written to a CSV file } LogLevel logLevel = NLog.LogLevel.Info; switch (category) { case LogEntryCategories.Debug: logLevel = NLog.LogLevel.Debug; break; case LogEntryCategories.Error: logLevel = NLog.LogLevel.Error; break; case LogEntryCategories.Fatal: logLevel = NLog.LogLevel.Fatal; break; case LogEntryCategories.Info: logLevel = NLog.LogLevel.Info; break; case LogEntryCategories.Trace: logLevel = NLog.LogLevel.Trace; break; case LogEntryCategories.Warning: logLevel = NLog.LogLevel.Warn; break; default: logLevel = NLog.LogLevel.Error; break; } LogEventInfo newLogEntry = new LogEventInfo(logLevel, "", message); newLogEntry.Properties["category"] = category.ToString(); //custom field 'category' newLogEntry.Properties["codeLine"] = sourceLineNumber; //custom field 'codeLine' newLogEntry.Exception = exception; applicationLogger.Log(newLogEntry); if (category == LogEntryCategories.Error || category == LogEntryCategories.Fatal) //only store errors { lock (errorListLock) { if (LatestErrors == null) { LatestErrors = new List <LogEntry>(); } while (LatestErrors.Count > 1000) { LatestErrors.RemoveAt(0); } LatestErrors.Add(new ErrorLogEntry() { Category = category.ToString(), ExceptionText = exception?.ToString(), Message = message, Time = DateTime.Now, LogLevel = logLevel.ToString() }); } } //log event OnLogEvent?.Invoke(null, new ErrorLogEntry() { Category = category.ToString(), ExceptionText = exception.ToString(), Message = message, LogLevel = logLevel.ToString(), Time = DateTime.Now, CodeLine = sourceLineNumber.ToString() }); }
public static void CreateNewLogMessage(object sender, string message) { OnLogEvent?.Invoke(sender, new LogEventArgs(message)); }
public static void WriteLine(string message, params object[] args) { OnLogEvent?.Invoke(null, string.Format(message, args: args)); }
public static void Log(object sender, string message, LogLevel level = LogLevel.Debug, bool writeLine = true) { var lineBuilder = new StringBuilder(); var lineColour = Colour.Normal; switch (level) { case LogLevel.Debug: lineBuilder.Append("[DEB] "); lineColour = Colour.White; break; case LogLevel.Info: lineBuilder.Append("[INF] "); lineColour = Colour.Green; break; case LogLevel.Message: lineBuilder.Append("[MSG] "); lineColour = Colour.Blue; break; case LogLevel.Irc: lineBuilder.Append("[IRC] "); lineColour = Colour.Normal; break; case LogLevel.Warning: lineBuilder.Append("[WRN] "); lineColour = Colour.Yellow; break; case LogLevel.Error: lineBuilder.Append("[ERR] "); lineColour = Colour.Red; break; } lineBuilder.Append(prefix); var time = DateTime.Now.ToString("[MMM dd - HH:mm:ss.fff] "); if (sender != null) { var location = $"[{sender.GetType().Name.Truncate(16)}-{sender.GetHashCode():X4}] "; lineBuilder.Insert(0, (time + location).PadRight(prefixLength)); } else { lineBuilder.Insert(0, time.PadRight(prefixLength)); } lineBuilder.Append(message); lock (lockObj) { WriteToConsole(lineColour, level, lineBuilder); if ((level == LogLevel.Error || level == LogLevel.Warning)) { OnLogEvent?.Invoke(lineBuilder.ToString(), level); } WriteToLogFile(lineBuilder, writeLine); } }
public static void DoLog(object sender, LogEventArgs args) { OnLogEvent?.Invoke(sender, args); }