.NET 运行时
.NET: 基于 .NET 软件框架的 Microsoft 技术。运行时: 运行未编译为机器语言的应用所需的环境。
54 个问题
尊敬的先生, 我收到以下异常 System.TypeInitializationException:“'ScheduleDataProcessor.Logger'的类型初始值设定项引发了异常。 内部异常 NotSupportedException:不支持给定路径的格式。 请找到以下代码:
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;
}
}
namespace ScheduleDataProcessor
{
public class Logger
{
private static StreamWriter sw;
private static String logDirectory;
static string outputPath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\Error\";
public static void LogMessage(String message)
{
Logger.Append(message);
}
// constructor for static resources
static Logger()
{
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);
}
public static void Append(String message)
{
try
{
lock (sw)
{
if (System.Configuration.ConfigurationSettings.AppSettings["WRITELOG"].Equals("true"))
{
sw.WriteLine("Timestamp : " + System.DateTime.Now + " :: " + message);
sw.Flush();
}
}
}
catch
{
// do nothing
}
}
}
Note:此问题总结整理于: System.TypeInitializationException: 'The type initializer for 'ScheduleDataProcessor.Logger' threw an exception.'
根据我的测试,我重现了您的问题。
我们还需要检查您的 bin/debug 文件夹中是否存在 Error 文件夹。
请进行以下更改:
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);
}
希望这对你有所帮助。 如果回复有帮助,请单击“接受答案”并投赞成票。
注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。