Example #1
0
        /// <summary>
        /// <para>There are max 3 retries.</para>
        /// <para>You should not do your own error handling in the upper class. This is done for you. If you want to do that anyway, throw the exception, so the flow is respected.</para>
        /// <para>You should suscribe to LogWriteException.</para>
        /// </summary>
        /// <param name="entry"></param>
        /// <param name="json"></param>
        private void InvokeWriteLogEntry(Entry entry, string json)
        {
            Exception logException = null;

            if (WriteLogEntry == null)
            {
                logException = new Exception("You must suscribe to WriteLogEntry in the upper class!\nThere you do the actual writing of the entry (to file, to mail, to database, ...).");
            }
            else
            {
                for (int tryCount = 0; tryCount != 3; tryCount++)
                {
                    try {
                        var ea = new WriteLogEntryEventArgs(entry, json);
                        WriteLogEntry.Invoke(this, ea);

                        if (LogEntryWritten != null)
                        {
                            LogEntryWritten(this, ea);
                        }
                        return;
                    } catch (Exception ex) {
                        logException = ex;
                        Thread.Sleep(100); // Some write stuff (like to file) possibly needs a bit of a timout before trying again.
                    }
                }
            }

            //Logs the exception to Debug, regardless the settings. Invokes LogWriteException if it has handlers in its invocationlist.
            InvokeLogWriteException(LogWriteExceptionToDebug(json, logException, null));
        }
Example #2
0
        private void MailLogger_WriteLogEntry(object sender, WriteLogEntryEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(SMTPServer))
            {
                throw new ArgumentException("Host must be filled in.");
            }
            if (string.IsNullOrWhiteSpace(Password))
            {
                throw new ArgumentException("Password must be filled in.");
            }
            if (string.IsNullOrWhiteSpace(FromEMailAddress))
            {
                throw new ArgumentException("FromEMailAddress must be filled in.");
            }
            if (string.IsNullOrWhiteSpace(ToEMailAddress))
            {
                throw new ArgumentException("ToEMailAddress must be filled in.");
            }

            var client = SMTPServerPort < 1 ? new SmtpClient(SMTPServer) : new SmtpClient(SMTPServer, SMTPServerPort);

            client.EnableSsl      = UseTLS_SSL;
            client.Timeout        = _mailSendTimeout;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            string username = Username;

            if (!string.IsNullOrWhiteSpace(username))
            {
                username = FromEMailAddress;
            }

            if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(Password))
            {
                client.UseDefaultCredentials = false;
                client.Credentials           = new NetworkCredential(username, Password);
            }

            var msg = new MailMessage(ToEMailAddress, FromEMailAddress, _subjectPrefix + " level " + e.Entry.Level, e.JSON);

            msg.SubjectEncoding = msg.BodyEncoding = UTF8Encoding.UTF8;
            msg.IsBodyHtml      = true;
            //Set the priority depending on the level.
            msg.Priority = e.Entry.Level > Level.Info ? (e.Entry.Level > Level.Warning ? MailPriority.High : MailPriority.Normal) : MailPriority.Low;
            msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            client.Send(msg);
        }
Example #3
0
        private void FileLogger_WriteLogEntry(object sender, WriteLogEntryEventArgs e)
        {
            bool mutexCreated;
            var  m = new Mutex(true, "SizingServers.Log.FileLogger", out mutexCreated); //Handle multiple processes writing to the same file.

            if (mutexCreated || m.WaitOne())
            {
                try {
                    if (!Directory.Exists(LogDir))
                    {
                        Directory.CreateDirectory(LogDir);
                    }

                    using (var sw = new StreamWriter(CurrentLogFile, true)) sw.WriteLine(e.JSON);
                } finally {
                    m.ReleaseMutex();
                }
            }
        }