简体   繁体   English

在Windows CE C#中记录错误

[英]Error logging in Windows CE C#

I'm writing a Windows CE App and want to implement error logging to a .txt file. 我正在编写Windows CE应用程序,并希望将错误日志记录实现为.txt文件。

I've done research and saw this example: 我做过研究并看到了这个例子:

public static void Logs(string fileName, string methodName, string message)
    {
        try
        {
            if (!string.IsNullOrEmpty(message))
            {
                using (FileStream file = new FileStream(Application.StartupPath + "\\Log.txt", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    StreamWriter streamWriter = new StreamWriter(file);
                    streamWriter.WriteLine((((System.DateTime.Now + " - ") + fileName + " - ") + methodName + " - ") + message);
                    streamWriter.Close();
                }
            }
        }
        catch
        {
        }
    } 

private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            int a = 10, b = 0;
            int result = a / b; //runtime Exception will throw
        }
        catch (Exception ex)
        {
            Logs(this.GetType().Name, "button1_Click()", ex.Message.ToString());
        }
    }

That works but only in Windows Forms. 这只适用于Windows窗体。 I'm getting this error in Windows CE: 我在Windows CE中收到此错误:

'System.Windows.Application' does not contain a definition for 'StartupPath'

Ok, so I know I have to use this: 好的,所以我知道我必须使用这个:

Path.GetDirectoryName (Assembly.GetExecutingAssembly ().GetName ().CodeBase);

But I'm not sure how. 但我不确定如何。 Still learning so please don't mark this as duplicate. 仍在学习,请不要将其标记为重复。 Can someone rather just explain to me exactly where and how to use this in Windows CE app? 有人可以在Windows CE应用程序中向我详细解释在哪里以及如何使用它吗?

Thanks in advance. 提前致谢。

在此输入图像描述

You can write a function that returns the path of the executing assembly... just like: 您可以编写一个函数来返回正在执行的程序集的路径...就像:

public static string GetCurrentApplicationPath() 
{
  return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}

and use this function in place of Application.StartupPath 并使用此函数代替Application.StartupPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM