从脚本背景开始,我开始使用c#和.net。我创建了一个创建文本文件的类,注意到一个过程已完成。我在控制台应用程序中实例化这个类,并在最后调用写入过程。我想要做的是在发生错误或警告时跟踪所有其他课程,并在通知中反映该事件。具有“全局”设置的实例类
我已经用public static bools实现了这个,所以无论哪个类正在运行,当发生catch时,我都可以将bool更新为true。虽然这是错误的,但我还是觉得,现在我还不知道应该怎么做。
任何人都可以帮忙吗? 代码示例:
namespace Logging
{
public class Notice
{
public static bool HasWarning { get; set; }
public static bool HasError { get; set; }
public string ProcessName {get; set;}
public enum Status
{
[Description("Complete Success")]
Complete_Success,
[Description("Complete with Warnings")]
Complete_With_Warnings,
[Description("Error Incomplete")]
Error_Incomplete
}
public void EndProcess(Status status)
{
StringBuilder sb = new StringBuilder();
sb.Append(string.Format("{0} is Complete!", ProcessName));
sb.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
sb.Append(string.Format("Completion Status: {0}", ToStringEnums(status)));
sb.Append(Environment.NewLine);
sb.Append(string.Format("Completion Time: {0}", DateTime.Now.ToString("MM/dd/yy H:mm:ss")));
File.WriteAllText(string.Format(@"{0}\EndProcess.txt", Directory.GetCurrentDirectory()), sb.ToString());
}
这就是我称之为:
private static void WriteNotice()
{
Notice newNotice = new Notice();
newNotice.ProcessName = "NCOA";
if (Notice.HasError == true)
newNotice.EndProcess(Notice.Status.Error_Incomplete);
else if (Notice.HasWarning == true)
newNotice.EndProcess(Notice.Status.Complete_With_Warnings);
else
newNotice.EndProcess(Notice.Status.Complete_Success);
}
如果您分享了您的代码样本,我们可能会发表更多评论。 –