写入日志文件c#

Write in Log file c#

我有一个代码,其中我从 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
             }

当我尝试读取文件名路径时出现问题。我无法在 catch "section" 中读取它。我是 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
                   }
           }
       }