Click here to Skip to main content
16,018,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");
            }
        }
    }
}


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]
Posted
Updated 17-Feb-11 0:28am
v3

1 solution

Hope N-Log[^]article might help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900