Пример #1
0
        public static void Configure()
        {
            var config = new LoggingConfiguration();
            var fileTarget = new FileTarget();

            fileTarget.FileName = Path.Combine(Util.get_log_folder(), "btnet_log.txt");
            fileTarget.ArchiveNumbering = ArchiveNumberingMode.Date;
            fileTarget.ArchiveEvery = FileArchivePeriod.Day;
            config.AddTarget("File", fileTarget);

            var mailTarget = new MailTarget
            {
                UseSystemNetMailSettings = true,
                To = Util.get_setting("ErrorEmailTo", ""),
                From = Util.get_setting("ErrorEmailFrom", ""),
                Subject = "BTNET Error Notification",
                Layout = "${machinename}${newline} ${date} ${newline} ${newline} ${message} ${newline}  ${exception} ${newline}"
            };
            config.AddTarget("Mail", mailTarget);

            //Turn logging on/off based on the LogEnabled setting
            var logLevel = Util.get_setting("LogEnabled", "1") == "1" ? LogLevel.Trace: LogLevel.Off;
            config.LoggingRules.Add(new LoggingRule("*", logLevel, fileTarget));

            var emailLogLevel = Util.get_setting("ErrorEmailEnabled", "1") == "1" ? LogLevel.Fatal : LogLevel.Off;
            config.LoggingRules.Add(new LoggingRule("*", emailLogLevel, mailTarget));

            LogManager.Configuration = config;
        }
Пример #2
0
    static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Setting up the target...");
            MailTarget target = new MailTarget();

            target.SmtpServer = "192.168.0.15";
            target.From = "*****@*****.**";
            target.To = "*****@*****.**";
            target.Subject = "sample subject";

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);

            Console.WriteLine("Sending...");
            Logger logger = LogManager.GetLogger("Example");
            Console.WriteLine("Sent.");
            logger.Debug("log message");
        }
        catch (Exception ex)
        {
            Console.WriteLine("EX: {0}", ex);
                
        }
    }
        private static void ConfigureEmailLogger(LoggingConfiguration config)
        {
            var emailTarget = new MailTarget();
            config.AddTarget("mail", emailTarget);

            emailTarget.Body = "${longdate} ${message} ${newline} ${onexception:EXCEPTION OCCURRED\\:${exception:format=tostring}}";
            emailTarget.From = "*****@*****.**";
            emailTarget.Html = false;
            emailTarget.Subject = "TemplateProject .Net Log Error ${date:format=yyyyMMddHHmmss}";
            emailTarget.To = "*****@*****.**";

            emailTarget.SmtpServer = "smtp.gmail.com";
            emailTarget.SmtpPort = 587;
            emailTarget.SmtpAuthentication = SmtpAuthenticationMode.Basic;
            emailTarget.SmtpUserName = "******";
            emailTarget.SmtpPassword = "******";
            emailTarget.EnableSsl = true;

            var emailRule = new LoggingRule("*", LogLevel.Error, emailTarget);
            config.LoggingRules.Add(emailRule);
        }
Пример #4
0
    static void Main(string[] args)
    {
        try
        {
            NLog.Internal.InternalLogger.LogToConsole = true;
            NLog.Internal.InternalLogger.LogLevel = LogLevel.Trace;
            Console.WriteLine("Setting up the target...");
            MailTarget target = new MailTarget();

            target.SmtpServer = "192.168.0.15";
            target.From = "*****@*****.**";
            target.To = "*****@*****.**";
            target.Subject = "sample subject";
            target.Body = "${message}${newline}";

            BufferingTargetWrapper buffer = new BufferingTargetWrapper(target, 5);

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(buffer, LogLevel.Debug);

            Console.WriteLine("Sending...");
            Logger logger = LogManager.GetLogger("Example");
            logger.Debug("log message 1");
            logger.Debug("log message 2");
            logger.Debug("log message 3");
            logger.Debug("log message 4");
            logger.Debug("log message 5");
            logger.Debug("log message 6");
            logger.Debug("log message 7");
            logger.Debug("log message 8");

            // this should send 2 mails - one with messages 1..5, the other with messages 6..8
            Console.WriteLine("Sent.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("EX: {0}", ex);
                
        }
    }
Пример #5
0
 public void DefaultSmtpClientTest()
 {
     var mailTarget = new MailTarget();
     var client = mailTarget.CreateSmtpClient();
     Assert.IsType(typeof(MySmtpClient), client);
 }
Пример #6
0
        public void MailTarget_UseSystemNetMailSettings_False_ReadFromFromConfigFile()
        {

            var mmt = new MailTarget()
            {
                From = null,
                To = "*****@*****.**",
                SmtpServer = "server1",
                SmtpPort = 27,
                Body = "${level} ${logger} ${message}",
                UseSystemNetMailSettings = false,
                SmtpSection = new SmtpSection { From = "*****@*****.**" }
            };
            Assert.Null(mmt.From);

            Assert.Throws <NLogConfigurationException>(() => mmt.Initialize(null));

        }
Пример #7
0
        public void MailTarget_UseSystemNetMailSettings_True_ReadFromFromConfigFile()
        {

            var mmt = new MailTarget()
            {
                From = null,
                To = "*****@*****.**",
                SmtpServer = "server1",
                SmtpPort = 27,
                Body = "${level} ${logger} ${message}",
                UseSystemNetMailSettings = true,
                SmtpSection = new SmtpSection { From = "*****@*****.**" }
            };
            Assert.Equal("'*****@*****.**'", mmt.From.ToString());

            mmt.Initialize(null);

            Assert.Equal("'*****@*****.**'", mmt.From.ToString());
        }
Пример #8
0
        static Log()
        {
            try
            {
                // Настройка конфигурации логгера.
                _logger = LogManager.GetLogger("AutoCAD_PIK_Manager");
                string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                var config = new LoggingConfiguration();

                //// Локальный лог
                //var fileLocalTarget = new FileTarget();
                //fileLocalTarget.FileName = Path.Combine(assemblyFolder, "Log.txt");
                //fileLocalTarget.Layout = "${longdate}_${level}:  ${message} ${exception:format=ToString,StackTrace}";
                //fileLocalTarget.ArchiveAboveSize = 200000;
                //fileLocalTarget.MaxArchiveFiles = 1;
                //config.AddTarget("localFile", fileLocalTarget);
                //var rule = new LoggingRule("*", LogLevel.Debug, fileLocalTarget);
                //config.LoggingRules.Add(rule);
                //LogManager.Configuration = config;

                // Серверный лог
                string serverLogPath = Path.Combine(PikSettings.ServerShareSettingsFolder, @"AutoCAD_PIK_Manager\Logs");
                var fileServerTarget = new FileTarget();
                fileServerTarget.FileName = string.Format(@"{0}\{1}-{2}.log", serverLogPath, Environment.UserName, Environment.MachineName);
                fileServerTarget.Layout = "${longdate}_${level}:  ${message} ${exception:format=ToString,StackTrace}";
                fileServerTarget.ArchiveAboveSize = 410152;
                fileServerTarget.MaxArchiveFiles = 1;
                fileServerTarget.ArchiveNumbering = ArchiveNumberingMode.Rolling;
                config.AddTarget("serverFile", fileServerTarget);
                var rule = new LoggingRule("*", LogLevel.Debug, fileServerTarget);
                config.LoggingRules.Add(rule);
                LogManager.Configuration = config;

                //AsyncTargetWrapper asyncWrapperSrvLog = new AsyncTargetWrapper(fileServerTarget);
                //asyncWrapperSrvLog.OverflowAction = AsyncTargetWrapperOverflowAction.Discard;
                //asyncWrapperSrvLog.QueueLimit = 100;
                //config.AddTarget("LogAsync", asyncWrapperSrvLog);
                //// Define rules
                //LoggingRule ruleSrvLogAsync = new LoggingRule("*", LogLevel.Error, asyncWrapperSrvLog);
                //config.LoggingRules.Add(ruleSrvLogAsync);

                // mail
                var mailTarget = new MailTarget();
                mailTarget.To = "[email protected]; " + PikSettings.PikFileSettings.MailCADManager;
                mailTarget.From = Environment.UserName + "@pik.ru";
                mailTarget.Subject = string.Format("Сообщение от {0}, AutoCAD_PIK_Manager", Environment.UserName);
                mailTarget.SmtpServer = "ex20pik.picompany.ru";
                mailTarget.Body = "${longdate} ${message} ${exception:format=ToString,StackTrace}";

                //config.AddTarget("mail", mailTarget);
                //rule = new LoggingRule("*", LogLevel.Error, mailTarget);
                //config.LoggingRules.Add(rule);

                // Set up asynchronous database logging assuming dbTarget is your existing target
                AsyncTargetWrapper asyncWrapperMail = new AsyncTargetWrapper(mailTarget);
                asyncWrapperMail.OverflowAction = AsyncTargetWrapperOverflowAction.Discard;
                asyncWrapperMail.QueueLimit = 100;
                config.AddTarget("MailAsync", asyncWrapperMail);
                // Define rules
                LoggingRule ruleAsync = new LoggingRule("*", LogLevel.Error, asyncWrapperMail);
                config.LoggingRules.Add(ruleAsync);

                LogManager.Configuration = config;
            }
            catch
            {
            }
        }
Пример #9
0
 public void DefaultSmtpClientTest()
 {
     var mailTarget = new MailTarget();
     var client = mailTarget.CreateSmtpClient();
     Assert.IsInstanceOfType(client, typeof(MySmtpClient));
 }