在日志文件中使用NLog的问题 [英] Problem using NLog with log files

查看:44
本文介绍了在日志文件中使用NLog的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用nlog创建错误日志文件?

我的代码如下:


How to create error log file using nlog??

my code is below:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using NLog.Targets;
using NLog.Config;
//using NLog.Win32.Targets;
using System.Diagnostics;
using System.Threading;

namespace Nlog
{
    class Program
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        static void Main(string[] args)
        {
            //WriteLogEvent();
            LogManager.ThrowExceptions = true;
            try
            {
                ProcessStartInfo procInfo = new ProcessStartInfo();
                procInfo.UseShellExecute = true;
                procInfo.FileName = @"PortfolioIntegrationServer.exe";  //The file in that DIR.
                procInfo.WorkingDirectory = @"E:\Program Files\Numerix\Numerix Portfolio\4.1\Bi\"; //The working DIR.
                //procInfo.Verb = "runas";
                Process p = Process.Start(procInfo);
            }
            catch (Exception ex)
            {
                string basedir = @"D:\Errorfileold\";
                // Step 1. Create configuration object 
                LoggingConfiguration config = new LoggingConfiguration();
                // Step 2. Create targets and add them to the configuration 
                ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();
                config.AddTarget("console", consoleTarget);
                FileTarget fileTarget = new FileTarget();
                config.AddTarget("file", fileTarget);
                // Step 3. Set target properties 
                consoleTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
                fileTarget.FileName = basedir + "/file.txt";
                fileTarget.Layout = "${message}";
                // Step 4. Define rules
                LoggingRule rule1 = new LoggingRule("*", LogLevel.Debug   , consoleTarget);
                config.LoggingRules.Add(rule1);
                LoggingRule rule2 = new LoggingRule("*", LogLevel.Debug  , fileTarget);
                config.LoggingRules.Add(rule2);
                // Step 5. Activate the configuration
                LogManager.Configuration = config;
                // Example usage
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.Trace("trace log message");
                logger.Debug("debug log message");
                logger.Info(" started");
                logger.ErrorException("", ex);
                logger.Warn("warn log message");
               // logger.Error(FormatException);
                logger.Fatal("fatal log message");
            }
        }
    }
}



我需要在错误日志文件中找不到错误消息文件... plz帮助

[edit]添加了代码块,删除了虚假的换行符,使主题变得更加相关-OriginalGriff [/edit]



I need the error msg file not found in my error log file...plz help

[edit]Code block added, spurious newlines removed, subject made more relevant - OriginalGriff[/edit]

推荐答案

{date:format = HH \\:MM \\ :ss}
{date:format=HH\\:MM\\:ss}


{logger}


{message}; fileTarget.FileName = basedir +"/file.txt"; fileTarget.Layout ="
{message}"; fileTarget.FileName = basedir + "/file.txt"; fileTarget.Layout = "


这篇关于在日志文件中使用NLog的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆