System.TypeInitializationException: 'The type initializer for 'ScheduleDataProcessor.Logger' threw an exception.'

manojkumar mahajan 21 Reputation points
2022-07-04T11:57:04.773+00:00

Dear Sir,
i am getting following exception
System.TypeInitializationException: 'The type initializer for 'ScheduleDataProcessor.Logger' threw an exception.'
inner exception NotSupportedException: The given path's format is not supported.
please find the following code:

namespace ScheduleDataProcessor
public partial class DataProcessor : Form
{
private System.Timers.Timer timer = new System.Timers.Timer();
DBConnection dbConnection = new DBConnection();
string pathBackup = ((object)ConfigurationManager.AppSettings["DATABACKUP"]).ToString();
public DataProcessor()
{
InitializeComponent();
}

    private void btnStartStop_Click(object sender, EventArgs e)  
    {  
        this.timer.Elapsed += new ElapsedEventHandler(this.OnElapsed);  
        this.timer.Interval = (double)Convert.ToInt64(((object)ConfigurationManager.AppSettings["Interval"]).ToString());  
        this.timer.Enabled = true;  
    }  

    private void OnElapsed(object sender, ElapsedEventArgs e)  
    {  
        this.timer.Enabled = false;  
        Logger.Append("Service is recalled");   //**<-----**--getting exception here****  
        try  
        {  
            this.LoopDataDirectory();  
        }  
       catch (Exception ex)  
        {  
            Logger.Append(ex.Message.ToString());  
        }  
        finally  
        {  
            this.timer.Enabled = true;  
        }  
    }  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,496 Reputation points Microsoft Vendor
    2022-07-05T08:24:25.533+00:00

    @ manojkumarmahajan-8101, thanks for the feedback, based on my test, I reproduced your problem.

    We also need to check if Error folder exists in your bin/debug folder.

    Please make the following change:

     static Logger()  
            {  
                if (!Directory.Exists(outputPath))  
                {  
                    Directory.CreateDirectory(outputPath);  
                }  
      
                logDirectory = outputPath +  System.Configuration.ConfigurationSettings.AppSettings["ERRORLOG"].ToString();;  
                if (File.Exists(logDirectory) && File.ReadAllBytes(logDirectory).Length >= 102400)  
                {  
                    File.Delete(logDirectory);  
                }  
                if (!File.Exists(logDirectory))  
                {  
                    FileStream fs = File.Create(logDirectory);  
                    fs.Close();  
                }  
                sw = File.AppendText(logDirectory);  
            }  
    

    Hope this could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful