当前位置: 首页>>代码示例>>C#>>正文


C# LoggingConfiguration.AddRuleForAllLevels方法代码示例

本文整理汇总了C#中NLog.Config.LoggingConfiguration.AddRuleForAllLevels方法的典型用法代码示例。如果您正苦于以下问题:C# LoggingConfiguration.AddRuleForAllLevels方法的具体用法?C# LoggingConfiguration.AddRuleForAllLevels怎么用?C# LoggingConfiguration.AddRuleForAllLevels使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NLog.Config.LoggingConfiguration的用法示例。


在下文中一共展示了LoggingConfiguration.AddRuleForAllLevels方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddRule_all

 public void AddRule_all()
 {
     var config = new LoggingConfiguration();
     config.AddTarget(new FileTarget {Name = "File"});
     config.AddRuleForAllLevels("File", "*a");
     Assert.NotNull(config.LoggingRules);
     Assert.Equal(1, config.LoggingRules.Count);
     var rule1 = config.LoggingRules.FirstOrDefault();
     Assert.NotNull(rule1);
     Assert.Equal(false, rule1.Final);
     Assert.Equal("*a", rule1.LoggerNamePattern);
     Assert.Equal(true, rule1.IsLoggingEnabledForLevel(LogLevel.Fatal));
     Assert.Equal(true, rule1.IsLoggingEnabledForLevel(LogLevel.Error));
     Assert.Equal(true, rule1.IsLoggingEnabledForLevel(LogLevel.Warn));
     Assert.Equal(true, rule1.IsLoggingEnabledForLevel(LogLevel.Info));
     Assert.Equal(true, rule1.IsLoggingEnabledForLevel(LogLevel.Debug));
     Assert.Equal(true, rule1.IsLoggingEnabledForLevel(LogLevel.Trace));
     Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Off));
 }
开发者ID:CharlieBP,项目名称:NLog,代码行数:19,代码来源:ConfigApiTests.cs

示例2: DontCrashWhenDateAndSequenceDoesntMatchFiles

        public void DontCrashWhenDateAndSequenceDoesntMatchFiles()
        {
            var tempDir = Path.Combine(Path.GetTempPath(), "DontCrashWhenDateAndSequenceDoesntMatchFiles-" + Guid.NewGuid());
            string logFile = Path.Combine(tempDir, "log.txt");
            try
            {
                // set log file access times the same way as when this issue comes up.
                Directory.CreateDirectory(tempDir);

                File.WriteAllText(logFile, "some content" + Environment.NewLine);
                var oldTime = DateTime.Now.AddDays(-2);
                File.SetCreationTime(logFile, oldTime);
                File.SetLastWriteTime(logFile, oldTime);
                File.SetLastAccessTime(logFile, oldTime);

                //write to archive directly
                var archiveDateFormat = "yyyyMMdd";
                var archiveFileNamePattern = Path.Combine(tempDir, "log-{#}.txt");
                var archiveFileName = archiveFileNamePattern.Replace("{#}", oldTime.ToString(archiveDateFormat));
                File.WriteAllText(archiveFileName, "some archive content");

                LogManager.ThrowExceptions = true;

                // configure nlog


                var fileTarget = new FileTarget("file")
                {
                    FileName = logFile,
                    ArchiveEvery = FileArchivePeriod.Day,
                    ArchiveFileName = "log-{#}.txt",
                    ArchiveNumbering = ArchiveNumberingMode.DateAndSequence,
                    ArchiveAboveSize = 50000,
                    MaxArchiveFiles = 7
                };


                var config = new LoggingConfiguration();
                config.AddRuleForAllLevels(fileTarget);
                LogManager.Configuration = config;

                // write
                var logger = LogManager.GetLogger("DontCrashWhenDateAndSequenceDoesntMatchFiles");
                logger.Info("Log message");

                LogManager.Flush();
            }
            finally
            {
                try
                {
                    if (File.Exists(logFile))
                        File.Delete(logFile);
                    Directory.Delete(tempDir, true);
                }
                catch (Exception)
                {
                }
            }
        }
开发者ID:NLog,项目名称:NLog,代码行数:60,代码来源:FileTargetTests.cs

示例3: ReplaceFileContentsOnEachWrite_CreateDirs

        public void ReplaceFileContentsOnEachWrite_CreateDirs(bool createDirs)
        {

            LogManager.ThrowExceptions = false;

            var tempPath = Path.Combine(Path.GetTempPath(), "dir_" + Guid.NewGuid().ToString());
            var logfile = Path.Combine(tempPath, "log.log");

            try
            {
                var target = new FileTarget
                {
                    FileName = logfile,
                    ReplaceFileContentsOnEachWrite = true,
                    CreateDirs = createDirs
                };
                var config = new LoggingConfiguration();

                config.AddTarget("logfile", target);

                config.AddRuleForAllLevels(target);

                LogManager.Configuration = config;

                ILogger logger = LogManager.GetLogger("A");
                logger.Info("a");

                Assert.Equal(createDirs, Directory.Exists(tempPath));
            }
            finally
            {
                if (File.Exists(logfile))
                    File.Delete(logfile);
                if (Directory.Exists(tempPath))
                    Directory.Delete(tempPath, true);
            }
        }
开发者ID:CharlieBP,项目名称:NLog,代码行数:37,代码来源:FileTargetTests.cs

示例4: HandleArchiveFileAlreadyExistsTest

        private void HandleArchiveFileAlreadyExistsTest(Encoding encoding, bool hasBom)
        {
            var tempDir = Path.Combine(Path.GetTempPath(), "HandleArchiveFileAlreadyExistsTest-" + Guid.NewGuid());
            string logFile = Path.Combine(tempDir, "log.txt");
            try
            {
                // set log file access times the same way as when this issue comes up.
                Directory.CreateDirectory(tempDir);


                File.WriteAllText(logFile, "some content" + Environment.NewLine, encoding);
                var oldTime = DateTime.Now.AddDays(-2);
                File.SetCreationTime(logFile, oldTime);
                File.SetLastWriteTime(logFile, oldTime);
                File.SetLastAccessTime(logFile, oldTime);

                //write to archive directly
                var archiveDateFormat = "yyyy-MM-dd";
                var archiveFileNamePattern = Path.Combine(tempDir, "log-{#}.txt");
                var archiveFileName = archiveFileNamePattern.Replace("{#}", oldTime.ToString(archiveDateFormat));
                File.WriteAllText(archiveFileName, "message already in archive" + Environment.NewLine, encoding);

                LogManager.ThrowExceptions = true;

                // configure nlog


                var fileTarget = new FileTarget("file")
                {
                    FileName = logFile,
                    ArchiveEvery = FileArchivePeriod.Day,
                    ArchiveFileName = archiveFileNamePattern,
                    ArchiveNumbering = ArchiveNumberingMode.Date,
                    ArchiveDateFormat = archiveDateFormat,
                    Encoding = encoding
                };


                var config = new LoggingConfiguration();
                config.AddTarget(fileTarget);
                config.AddRuleForAllLevels(fileTarget);

                LogManager.Configuration = config;

                var logger = LogManager.GetLogger("HandleArchiveFileAlreadyExistsTest");
                // write, this should append.
                logger.Info("log to force archiving");


                LogManager.Flush();
                AssertFileContents(archiveFileName, "message already in archive" + Environment.NewLine + "some content" + Environment.NewLine, encoding, hasBom);
            }
            finally
            {
                try
                {
                    if (File.Exists(logFile))
                        File.Delete(logFile);
                    Directory.Delete(tempDir, true);
                }
                catch (Exception)
                {
                }
            }
        }
开发者ID:NLog,项目名称:NLog,代码行数:65,代码来源:FileTargetTests.cs


注:本文中的NLog.Config.LoggingConfiguration.AddRuleForAllLevels方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。