写在日志文件c#

本文关键字:文件 日志 | 更新日期: 2023-09-27 18:06:38

我有一个代码,我从一个XML文件中读取一个路径,我必须在其中保存我的结果,如果程序中有一个错误,它停止,写下错误。

我的程序有这样的结构:

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
             }

问题来了,当我试图读取路径是filename。我看不懂" catch "部分。我是c#的新手。有简单的方法吗?

Thanks in advance

写在日志文件c#

try块之前声明您想要在catch块中使用的任何内容

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
                   }
           }
       }