简体   繁体   中英

Write in Log file c#

I have a code in which I read from a XML file a path in which I have to save my results, and in case there is an error in the program and it stops, write down the error.

My program has this structure:

namespace RiskFactorListUpdater
{
    public static class Program
    {
       public static void Main(string[] args)
        {
            try
            {
                //get the data
                filename=.....;
                // blah blah blah


                // more code

              }catch(Exception e)
               {
                //write in log file
             }

The problem comes when I try to read the path which is filename. I can't read it in the catch "section". I am a newbie in c#. Is there an easy way for doing this?

Thanks in advance

Declare anything you want to use in the catch block before the try block.

public static class Program
{
   public static void Main(string[] args)
    {
        string filename;
        try
        {
            //get the data
            filename=.....;
            // blah blah blah


            // more code

          }catch(Exception e)
           {
            //write in log file
         }
 you can use below method in proper formating way.
    public static class Program
        {
            public static string filename {get;set} 

            public static void Main(string[] args)
            {
                    filename="";
                    try
                    {      
                      filename=.....;
                    }
                   catch(Exception e)
                   {
                      //write in log file
                   }
           }
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM