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


C# Targets.FileTarget类代码示例

本文整理汇总了C#中NLog.Targets.FileTarget的典型用法代码示例。如果您正苦于以下问题:C# FileTarget类的具体用法?C# FileTarget怎么用?C# FileTarget使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


FileTarget类属于NLog.Targets命名空间,在下文中一共展示了FileTarget类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateLogger

        private static Logger CreateLogger()
        {
            var config = new LoggingConfiguration();

            var fileTarget = new FileTarget();
            config.AddTarget("file", fileTarget);

            fileTarget.FileName = "${basedir}\\AsteriskLog.txt";
            fileTarget.Layout = "${date:format=yyyy-MM-dd HH\\:mm\\:ss}: ${message}";
            fileTarget.ArchiveFileName = "${basedir}\\archives\\log.{#####}.txt";
            fileTarget.ArchiveAboveSize = 10240000;
            fileTarget.ArchiveNumbering = ArchiveNumberingMode.Sequence;

            var rule = new LoggingRule("*", LogLevel.Trace, fileTarget);
            config.LoggingRules.Add(rule);

            var errorTarget = new FileTarget();
            config.AddTarget("errorfile", errorTarget);

            errorTarget.FileName = "${basedir}\\Errors.txt";
            errorTarget.Layout = "${date:format=yyyy-MM-dd HH\\:mm\\:ss}: ${message}";
            errorTarget.ArchiveFileName = "${basedir}\\archives\\log.{#####}.txt";
            errorTarget.ArchiveAboveSize = 10240000;
            errorTarget.ArchiveNumbering = ArchiveNumberingMode.Sequence;

            var errorsRule = new LoggingRule("*", LogLevel.Error, errorTarget);
            config.LoggingRules.Add(errorsRule);

            LogManager.Configuration = config;

            return LogManager.GetCurrentClassLogger();
        }
开发者ID:Madster9000,项目名称:CallCenter,代码行数:32,代码来源:NLogContaner.cs

示例2: FileAppenderCache_Allocate

        public void FileAppenderCache_Allocate()
        {
            // Allocate on an Empty FileAppenderCache.
            FileAppenderCache emptyCache = FileAppenderCache.Empty;
            Assert.Throws<NullReferenceException>(() => emptyCache.AllocateAppender("file.txt"));

            // Construct a on non-empty FileAppenderCache.
            IFileAppenderFactory appenderFactory = SingleProcessFileAppender.TheFactory;
            ICreateFileParameters fileTarget = new FileTarget();
            String tempFile = Path.Combine(
                    Path.GetTempPath(),
                    Path.Combine(Guid.NewGuid().ToString(), "file.txt")
            );
            
            // Allocate an appender.
            FileAppenderCache cache = new FileAppenderCache(3, appenderFactory, fileTarget);
            BaseFileAppender appender = cache.AllocateAppender(tempFile);

            //
            // Note: Encoding is ASSUMED to be Unicode. There is no explicit reference to which encoding will be used 
            //      for the file. 
            //

            // Write, flush the content into the file and release the file.
            // We need to release the file before invoking AssertFileContents() method.
            appender.Write(StringToBytes("NLog test string."));
            appender.Flush();
            appender.Close();
            // Verify the appender has been allocated correctly.
            AssertFileContents(tempFile, "NLog test string.", Encoding.Unicode);
        }
开发者ID:daniefer,项目名称:NLog,代码行数:31,代码来源:FileAppenderCacheTests.cs

示例3: SimpleFileTest1

        public void SimpleFileTest1()
        {
            string tempFile = Path.GetTempFileName();
            try
            {
                FileTarget ft = new FileTarget();
                ft.FileName = SimpleLayout.Escape(tempFile);
                ft.LineEnding = LineEndingMode.LF;
                ft.Layout = "${level} ${message}";
                ft.OpenFileCacheTimeout = 0;

                SimpleConfigurator.ConfigureForTargetLogging(ft, LogLevel.Debug);

                logger.Debug("aaa");
                logger.Info("bbb");
                logger.Warn("ccc");
                LogManager.Configuration = null;
                AssertFileContents(tempFile, "Debug aaa\nInfo bbb\nWarn ccc\n", Encoding.UTF8);
            }
            finally
            {
                if (File.Exists(tempFile))
                    File.Delete(tempFile);
            }
        }
开发者ID:304NotModified,项目名称:NLog-1,代码行数:25,代码来源:FileTargetTests.cs

示例4: LoadLoggerToLoggingConfig

 public static void LoadLoggerToLoggingConfig(LoggingConfiguration logConfig, IConfiguration Configuration,string sectionString)
 {
     var fileLoggerSection = Configuration.GetSection(sectionString).GetChildren();
     foreach (var logging in fileLoggerSection)
     {
         var target = new FileTarget()
         {
             FileName = logging["fileName"],
             Name = logging["name"],
             Layout = logging["layoutFormat"]
         };
         var minLevel = logging["minLevel"] != null ? LogLevel.FromString(logging["minLevel"]) : null;
         LoggingRule rule = null;
         if (minLevel != null)
         {
             rule = new LoggingRule(logging["namePattern"], minLevel, target);
         }
         else
         {
             rule = new LoggingRule(logging["namePattern"], target);
         }
         var useLevels = logging["logLevel"];
         if(string.IsNullOrWhiteSpace(useLevels) == false)
         {
             var levels = useLevels.Split(',');
             foreach (var level in levels)
             {
                 rule.EnableLoggingForLevel(LogLevel.FromString(level));
             }
         }
         logConfig.AddTarget(target);
         logConfig.LoggingRules.Add(rule);
     }
 }
开发者ID:Sharelink,项目名称:BahamutZero,代码行数:34,代码来源:LoggerLoaderHelper.cs

示例5: ConfigureSharedFile

        private void ConfigureSharedFile(string mode)
        {
            FileTarget ft = new FileTarget();
            ft.FileName = "${basedir}/file.txt";
            ft.Layout = "${threadname} ${message}";
            ft.KeepFileOpen = true;
            ft.OpenFileCacheTimeout = 10;
            ft.OpenFileCacheSize = 1;
            ft.LineEnding = LineEndingMode.LF;

            switch (mode)
            {
                case "async":
                    SimpleConfigurator.ConfigureForTargetLogging(new AsyncTargetWrapper(ft, 100, AsyncTargetWrapperOverflowAction.Grow), LogLevel.Debug);
                    break;

                case "buffered":
                    SimpleConfigurator.ConfigureForTargetLogging(new BufferingTargetWrapper(ft, 100), LogLevel.Debug);
                    break;

                case "buffered_timed_flush":
                    SimpleConfigurator.ConfigureForTargetLogging(new BufferingTargetWrapper(ft, 100, 10), LogLevel.Debug);
                    break;

                default:
                    SimpleConfigurator.ConfigureForTargetLogging(ft, LogLevel.Debug);
                    break;
            }
        }
开发者ID:Enzyoh,项目名称:NLog,代码行数:29,代码来源:ConcurrentFileTargetTests.cs

示例6: initLog

        private void initLog()
        {
            var path = HostingEnvironment.MapPath("~/App_Data");

            var config = new LoggingConfiguration();

            var fileTarget = new FileTarget()
            {
                FileName = Path.Combine(path, "activity.log"),
                ArchiveFileName = Path.Combine(path, "activity.{#####}.log"),
                ArchiveAboveSize = 1024 * 1024,
                ArchiveNumbering = ArchiveNumberingMode.Sequence,
                ConcurrentWrites = false,
                Layout = "${longdate} | ${level} | ${logger} | ${message} ${exception:format=tostring}",
                AutoFlush = true,
                MaxArchiveFiles = 50
            };

            config.AddTarget("file", fileTarget);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, fileTarget));

            var traceTarget = new TraceTarget() { Layout = "${level} | ${logger} | ${message} ${exception:format=tostring}" };
            config.AddTarget("trace", traceTarget);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, traceTarget));

            LogManager.Configuration = config;
        }
开发者ID:lx223,项目名称:Q3,代码行数:27,代码来源:Startup.cs

示例7: TargetFile

        public static void TargetFile(string fileName, bool verbose)
        {
            var target = new FileTarget { Layout = LayoutFormat, FileName = fileName };

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, verbose ? LogLevel.Debug : LogLevel.Info);
            LogManager.ReconfigExistingLoggers();
        }
开发者ID:goldbillka,项目名称:Winium.StoreApps,代码行数:7,代码来源:Logger.cs

示例8: ConfigureNlog

        public void ConfigureNlog(Logger logger)
        {
            FileTarget target = new FileTarget();

            target.FileName = "${basedir}\\logs\\ICtrl.Log_${date:format=ddMMyyyy}.txt";
            target.KeepFileOpen = false;
            target.Encoding = "windows-1251";
            target.Layout = "${date:format=HH\\:mm\\:ss.fff}|${level:padding=5:uppercase=true}|${message}";

            AsyncTargetWrapper wrapper = new AsyncTargetWrapper();

            wrapper.WrappedTarget = target;
            wrapper.QueueLimit = 5000;
            wrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Block;

            logOn = GetLogLevel();

            switch (logOn)
            {
                case 1:
                    NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(wrapper, LogLevel.Info);
                    break;
                default:
                    NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(wrapper, LogLevel.Off);
                    break;
            }
        }
开发者ID:sokolnikov90,项目名称:ICTRL,代码行数:27,代码来源:Settings.cs

示例9: ConfigureLogging

        private void ConfigureLogging()
        {
            var fileTarget = new FileTarget {
                FileName = Path.Combine(GetLogFolder(), "log.xml"),
                ArchiveFileName = "log_{#####}.xml",
                ArchiveNumbering = ArchiveNumberingMode.Sequence,
                ArchiveAboveSize = 1024*1024,
                Layout = new Log4JXmlEventLayout()
            };

            var config = new LoggingConfiguration();
            config.AddTarget("file", fileTarget);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, fileTarget));

            var debuggerTarget = new DebuggerTarget();
            config.AddTarget("debugger", debuggerTarget);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, debuggerTarget));

            if (Debugger.IsAttached) {
                var udpTarget = new NetworkTarget {
                    Address = "udp4://localhost:962",
                    Layout = new Log4JXmlEventLayout()
                };
                config.AddTarget("udp", udpTarget);
                config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, udpTarget));
            }

            LogManager.Configuration = config;

            PresentationTraceSources.DataBindingSource.Listeners.Add(new NLogTraceListener());
        }
开发者ID:csharper2010,项目名称:Solutionizer,代码行数:31,代码来源:BootstrapperBase.cs

示例10: SetupLogging

    // ------------------------------------------
    //  Logs Management
    // ------------------------------------------

    protected void SetupLogging() {
      var path = ConfigManager.GetInstance().Find("debug.log-file", null);
      if (null == path) { return; }
      
      LoggingConfiguration config = new LoggingConfiguration();

      ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();
      consoleTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
      config.AddTarget("console", consoleTarget);

      FileTarget fileTarget = new FileTarget();
      fileTarget.FileName = path;
      fileTarget.Layout = "${message}";
      fileTarget.CreateDirs = true;
      config.AddTarget("file", fileTarget);

      LoggingRule rule1 = new LoggingRule("*", LogLevel.Info, consoleTarget);
      config.LoggingRules.Add(rule1);

      LoggingRule rule2 = new LoggingRule("*", LogLevel.Info, fileTarget);
      config.LoggingRules.Add(rule2);

      LoggingRule rule3 = new LoggingRule("*", LogLevel.Warn, consoleTarget);
      config.LoggingRules.Add(rule3);

      LoggingRule rule4 = new LoggingRule("*", LogLevel.Warn, fileTarget);
      config.LoggingRules.Add(rule4);

      // Ready
      LogManager.ReconfigExistingLoggers();
      LogManager.Configuration = config;
      Host.Log(this, "STARTING LOGGING:" + fileTarget.FileName);
    }
开发者ID:JpEncausse,项目名称:SARAH-Client-Windows,代码行数:37,代码来源:AddOn.cs

示例11: ConfigureSharedFile

        private void ConfigureSharedFile(string mode, string fileName)
        {
            var modes = mode.Split('|');

            FileTarget ft = new FileTarget();
            ft.FileName = "${basedir}/" + fileName;
            ft.Layout = "${message}";
            ft.KeepFileOpen = true;
            ft.OpenFileCacheTimeout = 10;
            ft.OpenFileCacheSize = 1;
            ft.LineEnding = LineEndingMode.LF;
            ft.ForceMutexConcurrentWrites = modes.Length == 2 && modes[1] == "mutex" ? true : false;

            var name = "ConfigureSharedFile_" + mode.Replace('|', '_') + "-wrapper";

            switch (modes[0])
            {
                case "async":
                    SimpleConfigurator.ConfigureForTargetLogging(new AsyncTargetWrapper(ft, 100, AsyncTargetWrapperOverflowAction.Grow) { Name = name }, LogLevel.Debug);
                    break;

                case "buffered":
                    SimpleConfigurator.ConfigureForTargetLogging(new BufferingTargetWrapper(ft, 100) { Name = name }, LogLevel.Debug);
                    break;

                case "buffered_timed_flush":
                    SimpleConfigurator.ConfigureForTargetLogging(new BufferingTargetWrapper(ft, 100, 10) { Name = name }, LogLevel.Debug);
                    break;

                default:
                    SimpleConfigurator.ConfigureForTargetLogging(ft, LogLevel.Debug);
                    break;
            }
        }
开发者ID:NLog,项目名称:NLog,代码行数:34,代码来源:ConcurrentFileTargetTests.cs

示例12: Main

        private static void Main(string[] args)
        {
            if (args.Length <= 0)
            {
                return;
            }

            var config = new LoggingConfiguration();
            var logfile = new FileTarget();
            var logdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "HearthstoneTracker");
            logdir = Path.Combine(logdir, "logs");
            var logfilename = Path.Combine(logdir, "updater.${date:format=yyyy-MM-dd}.txt");
            logfile.FileName = logfilename;
            logfile.CreateDirs = true;
            logfile.MaxArchiveFiles = 7;
            logfile.ArchiveEvery = FileArchivePeriod.Day;
            logfile.ConcurrentWrites = true;
            logfile.Layout =
                "${longdate}|${level:uppercase=true}|thread:${threadid}|${logger}|${message}${onexception:inner=${newline}${exception:format=tostring}}";

            config.AddTarget("logfile", logfile);
            config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, logfile));
            LogManager.Configuration = config;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new UpdateProgress(args));
        }
开发者ID:ProjectTako,项目名称:HearthstoneTracker,代码行数:28,代码来源:Program.cs

示例13: Configure

        private static void Configure(INewRelicConfig config)
        {
            config = config ?? NewRelicConfig.Instance;
            LoggingConfiguration loggingConfiguration = new LoggingConfiguration();
            ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget { Name = "Console" };
            loggingConfiguration.AddTarget("Console", consoleTarget);
            loggingConfiguration.LoggingRules.Add(new LoggingRule("*", ToNLogLevel(config.LogLevel), consoleTarget));

            // Store the LogLevel so it can be fetched by consumers
            LogLevel = config.LogLevel;

            if (config.LogFilePath.IsValidString() && config.LogFileName.IsValidString())
            {
                long archiveAboveSize = config.LogLimitInKiloBytes == 0 ? long.MaxValue : config.LogLimitInKiloBytes * 1024;
                FileTarget fileTarget = new FileTarget
                {
                    KeepFileOpen = true,
                    ConcurrentWrites = false,
                    FileName = Path.Combine(config.LogFilePath, config.LogFileName),
                    MaxArchiveFiles = 1,
                    ArchiveAboveSize = archiveAboveSize,
                    Name = "File",
                };

                loggingConfiguration.AddTarget("File", fileTarget);
                loggingConfiguration.LoggingRules.Add(new LoggingRule("*", ToNLogLevel(config.LogLevel), fileTarget));
            }

            NLogManager.Configuration = loggingConfiguration;
        }
开发者ID:edwinf,项目名称:newrelic_dotnet_sdk,代码行数:30,代码来源:Logger.cs

示例14: BuildLogger

    public static Logger BuildLogger()
    {
        var consoleTarget = new ColoredConsoleTarget
                                {
                                    Layout = "${message} ${exception:format=tostring}"
                                };

        var fileName = Path.Combine(Path.GetTempPath(), "NancyOnKayak", "WebsiteLog_${shortdate}.log");
        Console.WriteLine("Logging to:" + fileName);

        var fileTarget = new FileTarget
                             {
                                 FileName = fileName,
                                 Layout = "${longdate} ${message} ${exception:format=tostring}",
                                 AutoFlush = true,
                             };

        var config = new LoggingConfiguration
                         {
                             LoggingRules =
                                 {
                                     new LoggingRule("*", LogLevel.Debug, consoleTarget),
                                     new LoggingRule("*", LogLevel.Debug, fileTarget),
                                 }
                         };

        config.AddTarget("file", fileTarget);
        config.AddTarget("console", consoleTarget);

        LogManager.Configuration = config;
        return LogManager.GetLogger("");
    }
开发者ID:rwecho,项目名称:Experiments,代码行数:32,代码来源:LoggingFactory.cs

示例15: SetupNLog

        private static Logger SetupNLog(NLogConfigurationApi config)
        {
            var logDirectory = string.IsNullOrEmpty(config.LogDirectory) ?
                "${basedir}/Logs" :
                config.LogDirectory;

            var fileTarget = new FileTarget
            {
                Name = "FileTarget",
                Layout = "${message}",
                ConcurrentWrites = false,
                FileName = new SimpleLayout(Path.Combine(logDirectory, "current.log")),
                ArchiveEvery = config.ArchivePeriod,
                ArchiveNumbering = ArchiveNumberingMode.Sequence,
                MaxArchiveFiles = config.MaxArchiveFiles,
                ArchiveFileName = new SimpleLayout(Path.Combine(logDirectory,"archive/{####}.log"))
            };
            var asyncWrapper = new AsyncTargetWrapper(fileTarget)
            {
                Name = "AsyncWrapper"
            };

            var loggingConfiguration = new LoggingConfiguration();
            loggingConfiguration.AddTarget(LoggerName, asyncWrapper);
            loggingConfiguration.LoggingRules.Add(new LoggingRule("*", LevelToNLogLevel(config.MinimumLogLevel), asyncWrapper));

            LogManager.Configuration = loggingConfiguration;

            return LogManager.GetLogger(LoggerName);
        }
开发者ID:oylers,项目名称:Spiffy,代码行数:30,代码来源:NLog.cs


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