System.TypeInitializationException创建日志对象时出错| C#

时间:2016-08-13 01:43:55

标签: c# winforms logging locking

我正在创建一个新项目,只是写了下面的Log文件,但遇到了unhandled exception of type 'System.TypeInitializationException' occured in My_Project.exe

内部异常表明{"Index (zero based) must be greater than or equal to zero and less than the size of the argument list."}

首次写入Log时发生错误。例如,当我使用以下代码时,程序崩溃并显示错误消息。

using System;
using System.Windows.Forms;
using My_Project.forms;

namespace My_Project {
    public static class Program {
        [STAThread]
        public static void Main() {
            Utility.MyLog.Write("Test");   //<-- ERROR OCCURS HERE
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Window());
        }
    }
}

这是Log实例化的地方..

public static class Utility
{
    // DEFAULT LOG OUTPUT.
    public static Log MyLog = new Log();

    ...
}

这是我的Log.cs文件,到目前为止还是完整的。

using System;
using System.IO;
using System.Reflection;
using My_Project.Properties;

// STATIC USING MEMBERS
using static My_Project.LogFormat;
using static System.IO.File;
using static My_Project.Utility;

namespace My_Project {
    public enum LogFormat { None, Message, Evaluate, Process, Result }

    public sealed class Log {
        // USED TO LOCK THE OUTPUT THREAD WHEN WRITING TO A FILE.
        private readonly object _lock = new object();

        // DEFAULT VARIABLES
        public readonly string FileName;
        public readonly string FilePath;

        // DEFAULT CONSTRUCTOR
        public Log(string fileName = null, string filePath = null) {
            FileName = fileName ?? Resources.DefaultLogFileName;

            var directoryInfo = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;
            if (directoryInfo != null)
                filePath = Path.Combine(filePath ?? directoryInfo.FullName, Resources.DefaultLogFilePath, FileName);
            FilePath = filePath;

            var projectName = Assembly.GetCallingAssembly().GetName().Name.ToUpper();
            Write("Hello World!");
        }

        // WRITES TO A TXT FILE, AND AUTO-CREATES A FILE IF IT DOESN'T EXIST.
        public void Write(string message) {
            lock (_lock) {
                try {
                    AppendAllText(FilePath, message);
                } catch (DirectoryNotFoundException ex) {
                    Console.WriteLine(string.Format(Resources.Error_CannotFindLogFile), ex.Message);
                    throw;
                }
            }
        }

        // OPENS THE LOG FILE IN NOTEPAD.
        public void Open() => OpenProgram(FilePath);

        // CLEARS ALL CONTENTS FROM LOG.TXT.
        public void Clear() => WriteAllText(FilePath, string.Empty);
    }
}

我做错了什么?

3 个答案:

答案 0 :(得分:0)

不确定异常发生在哪一行,因为无法调试代码但是你得到异常导致初始化Log()在构造函数或Write()方法中失败了在内部构造函数中,您正在调用Write("Hello World!");。具体如下。您应该在Log类构造函数中放置一个断点并逐步执行它,看看它究竟在哪里失败。您已经有了原因(请参阅内部异常消息Index (zero based) must be greater than or equal)。

public static Log MyLog = new Log(); 

答案 1 :(得分:0)

我猜它是string.Format(Resources.Error_CannotFindLogFile)的行。格式是不必要的,因为WriteLine为你做了。

答案 2 :(得分:0)

原来是我的资源文件中出现了无效的文件路径错误。我有正确读入的文件夹名称。