Esempio n. 1
0
        /// <summary>
        /// Logs the specified error.
        /// </summary>
        public static bool Log2Instance(string error, int severity, string component, string instance)
        {
            try
            {
                DebugLog(error, component);
                if (null == error || null == component)
                {
                    return(false);
                }

                var computerName = Environment.MachineName;
                var processName  = GetProcessName() + "." + component;

                if (0 == computerName.Length)
                {
                    computerName = "UNKNOWN";
                }

                // and log if allowed
                if (ApplicationConfiguration.Instance.LogToDatabase > 0 && severity >= ApplicationConfiguration.Instance.LogToDatabase)
                {
                    var myAppLog = new ApplicationLog
                    {
                        Component         = processName,
                        CurrentDbInstance = instance,
                        ErrorString       = error.Length > 2000 ? error.Substring(0, 2000) : error,
                        Server            = computerName,
                        Severity          = severity
                    };

                    myAppLog.Execute();
                }

                // now the file
                if (ApplicationConfiguration.Instance.LogToFile > 0 &&
                    severity >= ApplicationConfiguration.Instance.LogToFile &&
                    !string.IsNullOrEmpty(ApplicationConfiguration.Instance.ErrorFile))
                {
                    using (var stream = File.AppendText(ApplicationConfiguration.Instance.ErrorFile))
                    {
                        var formattedErrorMessage =
                            String.Format("{0}\tServer={1}\tSeverity: {2}\tComponent: {3}\tMessage: {4}",
                                          DateTime.Now.ToString("G"),
                                          Environment.MachineName,
                                          severity,
                                          processName,
                                          error);
                        stream.WriteLine(formattedErrorMessage);
                        stream.Flush();
                        stream.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                LogEvent(ex, new CallingMethod());
            }
            return(true);
        }
Esempio n. 2
0
        public void TestApplicationLog()
        {
            var myLog = new ApplicationLog
            {
                Component   = "DbClassLibrary",
                ErrorString = "This is a test entry",
                Server      = "CHADTBD2",
                Severity    = 1
            };

            Assert.IsTrue(myLog.Execute(), myLog.LastError);
            Assert.IsTrue(myLog.ApplicationLogID > -1);
        }