Exemplo n.º 1
0
        private SmtpAppender CreateSmtpAppender(string appenderName, string subject, string mailFrom, string mailTo, string userName, string password)
        {
            var appender = new SmtpAppender();

            appender.Name = appenderName;

            appender.Authentication = SmtpAppender.SmtpAuthentication.Basic;
            appender.To             = mailFrom;
            appender.From           = mailTo;
            appender.Username       = userName;
            appender.Password       = password;
            appender.Subject        = subject;
            appender.SmtpHost       = "smtp.qq.com";
            appender.BufferSize     = 4;
            appender.Lossy          = true;
            appender.Evaluator      = new LevelEvaluator()
            {
                Threshold = Level.Error
            };

            PatternLayout layout = new PatternLayout();

            layout.ConversionPattern = "%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline";
            layout.ActivateOptions();

            appender.Layout = layout;

            appender.ActivateOptions();

            return(appender);
        }
Exemplo n.º 2
0
        /// <summary>
        /// For engine use only! Not thread safe
        /// </summary>
        /// <param name="prefix"></param>
        public void SetLogFilePrefix(string prefix)
        {
            Info(typeof(Logger).Name, "SetLogFilePrefix", "Changing log file -> Adding prefix: '" + prefix + "'");
            try
            {
                log4net.Repository.Hierarchy.Hierarchy h =
                    (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
                foreach (IAppender a in h.Root.Appenders)
                {
                    if (a is FileAppender)
                    {
                        FileAppender fa = (FileAppender)a;

                        FileInfo fileInfo    = new FileInfo(fa.File);
                        var      logFileName = string.Format("{0}-{1}", prefix, fileInfo.Name);

                        fa.File = Path.Combine(fileInfo.DirectoryName, logFileName);
                        fa.ActivateOptions();
                        break;
                    }
                    else if (a is SmtpAppender)
                    {
                        SmtpAppender sa = (SmtpAppender)a;
                        sa.Subject = sa.Subject + " " + prefix;
                        sa.ActivateOptions();
                    }
                }
            }
            catch (Exception ex)
            {
                Error(typeof(Logger).Name, "SetLogFilePrefix", ex, "Prefix not set - log entries are probably in the base log file");
            }
        }
Exemplo n.º 3
0
    private SmtpAppender GetSMTPAppender(string smtpHost, string From, string To, string CC, string subject, log4net.Core.Level threshhold)
    {
        SmtpAppender lAppender = new SmtpAppender();

        lAppender.Cc         = CC;
        lAppender.To         = To;
        lAppender.From       = From;
        lAppender.SmtpHost   = smtpHost;
        lAppender.Subject    = subject;
        lAppender.BufferSize = 512;
        lAppender.Lossy      = false;
        lAppender.Layout     = new
                               log4net.Layout.PatternLayout("%date{dd-MM-yyyy HH:mm:ss,fff} %5level [%2thread] %message (%logger{1}:%line)%n");
        lAppender.Threshold = threshhold;
        lAppender.ActivateOptions();
        return(lAppender);
    }
Exemplo n.º 4
0
        private static void UpdateLog4netConfig()
        {
            Hierarchy root = LogManager.GetRepository() as Hierarchy;

            if (root == null)
            {
                return;
            }

            SmtpAppender sa = root.Root.GetAppender("SmtpAppender") as SmtpAppender;

            if (sa == null)
            {
                return;
            }

            sa.To   = ExceptionHandler.ToEmail;
            sa.From = ExceptionHandler.FromEmail;
            sa.ActivateOptions();

            m_Logger.DebugFormat("Updated log4net SmtpAppender to address: {0}", sa.To);
        }