/// <summary> /// Private function that allows for passing whether or not the errors should recurse if the log has an error when writing. /// </summary> private static int WriteLog(string appSource, string message, ErrorEventID eventID, Exception causeExc, bool recurse) { string src; string msg; int eventtype; string callingfunction = ""; int errorID = -1; appSource = appSource != null ? appSource : "BadSource"; message = message != null ? message : "<Bad Message>"; try { src = "Website-" + (appSource.Length > 40 ? appSource.Substring(0, 40) : appSource); msg = message; eventtype = (int)eventID; } catch { src = "Website-" + appSource; msg = message.ToString(); eventtype = -1; } //Get the function that called this one try { StackTrace callStack = new StackTrace(); for (int i = 0; i < callStack.FrameCount; i++) { MethodBase mb = callStack.GetFrame(i).GetMethod(); callingfunction = mb.Name + "->" + callingfunction; } } catch { callingfunction = "<Unknown>"; } //Get the page URL string pageURL; try { pageURL = (HttpContext.Current != null && HttpContext.Current.Request != null) ? HttpContext.Current.Request.Url.ToString() : "<No Context or Request>"; } catch { pageURL = "<No Context or Request>"; } //Get the server name string serverName; try { serverName = System.Net.Dns.GetHostName(); } catch { serverName = "Unknown-Server"; } //Check if we should be logging errors to specific places based on the current configuration // Write error log to (Application) EVENT LOG if (eventID != ErrorEventID.LogErrorEvent && LogToEventLog) { try { if (!EventLog.SourceExists(src)) { // Create EventLog.CreateEventSource(src, "Application"); } if (causeExc != null) { EventLog.WriteEntry(src, msg + "\nServer: " + serverName + "\nPage URL: " + pageURL + "\nCalling Function: " + callingfunction + "\n" + GenerateExceptionInfoString(causeExc, false) + (causeExc.InnerException != null ? "\n---Inner Exception---\n" + GenerateExceptionInfoString(causeExc.InnerException, false) + "\n---End Inner Exception---" : ""), EventLogEntryType.Error, eventtype); } else { EventLog.WriteEntry(src, msg, EventLogEntryType.Error, eventtype); } } catch (Exception ex) { if (recurse) { WriteLog("ErrorLog-Event", "There was an error writing to the event log.", ErrorEventID.LogError, ex, false); } } } // Write error log to DATABASE if (eventID != ErrorEventID.ConnectionError && eventID != ErrorEventID.LogErrorSQL && LogToDatabase) { try { string logTableName; if (Array.IndexOf <string>(WebConfigurationManager.OpenWebConfiguration("/").AppSettings.Settings.AllKeys, "LogTableName") > -1) { logTableName = WebConfigurationManager.OpenWebConfiguration("/").AppSettings.Settings["LogTableName"].Value; } else { logTableName = "tblSYSAppErrorLogs"; } SQLDatabase sql = new SQLDatabase(); // sql.CommandTimeout = 120; string ms = msg; if (causeExc != null) { ms += "\n" + GenerateExceptionInfoString(causeExc, false) + (causeExc.InnerException != null ? "\n---Inner Exception---\n" + GenerateExceptionInfoString(causeExc.InnerException, false) + "\n---End Inner Exception---" : ""); ms = (ms.Length > 1500 ? ms.Substring(0, 1500) : ms); } else { ms = (ms.Length > 1500 ? ms.Substring(0, 1500) : ms); } ms = "Calling Function: " + callingfunction + "\n" + ms; sql.LogErrors = false; string sqlquery = "INSERT INTO [" + logTableName + "] (ApplicationSource,EventSource,Message,EventDate,Server,PageURL) " + " VALUES (@AppSrc, @EvtSrc, @Msg, GetDate(), @SvrName, @Page)"; //sql.NonQueryWithParams(sqlquery, new SQLDatabase.SQLParamList().Add("@AppSrc", src).Add("@EvtSrc", eventtype).Add("@Msg", ms).Add("@SvrName", serverName).Add("@Page", pageURL)); errorID = sql.QueryAndReturnIdentity(sqlquery, new SQLParamList().Add("@AppSrc", src).Add("@EvtSrc", eventtype).Add("@Msg", ms).Add("@SvrName", serverName).Add("@Page", pageURL)); if (sql.HasError) { WriteLog("ErrorLog-SQLLog", "There was an error writing a log to the SQL server database. SQL Query: " + sqlquery, ErrorEventID.LogErrorSQL, sql.ExceptionList[0], false); } } catch (Exception ex) { if (recurse) { WriteLog("ErrorLog-SQL", "There was an error writing to the SQL server database.", ErrorEventID.LogErrorSQL, ex, false); } } } //Write to error log to FILE if (eventID != ErrorEventID.LogErrorFile && LogToFile) { try { string path; if (Array.IndexOf <string>(WebConfigurationManager.OpenWebConfiguration("/").AppSettings.Settings.AllKeys, "LogFileDirectory") > -1) { path = WebConfigurationManager.OpenWebConfiguration("/").AppSettings.Settings["LogFileDirectory"].Value; } else { path = "~/Files/Logs/"; } path = HttpContext.Current.Server.MapPath(path); if (Directory.Exists(path)) { TextWriter tw = new StreamWriter(path + "ErrorLog-" + serverName + "-" + DateTime.Now.ToString("yyyyMMdd") + ".log", true); tw.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"); tw.WriteLine("Date: " + DateTime.Now.ToLongDateString()); tw.WriteLine("Time: " + DateTime.Now.ToLongTimeString()); tw.WriteLine("Server: " + serverName); tw.WriteLine("Page URL: " + pageURL); tw.WriteLine("Calling Function: " + callingfunction); tw.WriteLine("SMIOS Error Event ID: " + eventtype.ToString()); tw.WriteLine("Error Source: " + src); tw.WriteLine("Message: " + msg); if (causeExc != null) { tw.WriteLine("Exception:\n" + GenerateExceptionInfoString(causeExc, false)); if (causeExc.InnerException != null) { tw.WriteLine("Inner Exception:"); tw.WriteLine(Conversion.XMLEncodeString(GenerateExceptionInfoString(causeExc.InnerException, false))); } } tw.WriteLine(); tw.Close(); tw = new StreamWriter(path + "ErrorLog-" + serverName + "-" + DateTime.Now.ToString("yyyyMMdd") + ".xml", true); tw.WriteLine("<error>"); tw.WriteLine("\t<date>" + Conversion.DateTimeToXMLString(DateTime.Now) + "</date>"); tw.WriteLine("\t<server>" + Conversion.XMLEncodeString(serverName) + "</server>"); tw.WriteLine("\t<pageurl>" + Conversion.XMLEncodeString(pageURL) + "</pageurl>"); tw.WriteLine("\t<callfunction>" + Conversion.XMLEncodeString(callingfunction) + "</callfunction>"); tw.WriteLine("\t<smioseventid>" + Conversion.XMLEncodeString(eventtype.ToString()) + "</smioseventid>"); tw.WriteLine("\t<apperrorsource>" + Conversion.XMLEncodeString(src) + "</apperrorsource>"); tw.WriteLine("\t<message>" + Conversion.XMLEncodeString(msg) + "</message>"); if (causeExc != null) { tw.WriteLine(GenerateExceptionInfoString(causeExc, true)); if (causeExc.InnerException != null) { tw.WriteLine("\t<innerexception>"); tw.WriteLine(Conversion.XMLEncodeString(GenerateExceptionInfoString(causeExc.InnerException, true))); tw.WriteLine("\t</innerexception>"); } } tw.WriteLine("</error>"); tw.Close(); } } catch (Exception ex) { if (recurse) { WriteLog("ErrorLog-File", "There was an error writing to the log file.", ErrorEventID.LogError, ex, false); } } } return(errorID); }
/// <summary> /// Writes log information to the Application, database and flat file logs (if enabled). /// </summary> /// <param name="appSource">The source of the error. "Website-" will be automatically prepended to this. Max 40 chars.</param> public static int WriteLog(string appSource, string message, ErrorEventID eventID) { return(WriteLog(appSource, message, eventID, null, true)); }
/// <summary> /// Writes log information to the Application, database and flat file logs (if enabled). Includes information about the passed exception automatically. /// </summary> /// /// <param name="appSource">The source of the error. "Website-" will be automatically prepended to this. Max 40 chars.</param> public static int WriteLog(string appSource, string message, ErrorEventID eventID, Exception excInfo) { return(WriteLog(appSource, message, eventID, excInfo, true)); }
/// <summary> /// Returns a string value for the name of the event type. /// </summary> /// <param name="evt">The event ID.</param> public static string ErrorName(ErrorEventID evt) { return(ErrorName((int)evt)); }