C# 通过代码添加应用程序侦听器

C# 通过代码添加应用程序侦听器,c#,wcf,configuration,app-config,C#,Wcf,Configuration,App Config,我想把所有东西都转换成C代码 此配置所做的是将所有发生的异常自动记录到文件中,我不需要为此编写代码。我正在寻找一种简单的方法,将我的异常记录到svclog文件中,直到客户端将其发送给我进行调试。但我想删除app.config,只需使用C代码即可。我搜索了很多,所以我们无法自动执行此操作,但这是一种很好的方法: public static class myClass { [XmlRoot("Exception"), System.Xml.Serialization.XmlType("

我想把所有东西都转换成C代码



此配置所做的是将所有发生的异常自动记录到文件中,我不需要为此编写代码。我正在寻找一种简单的方法,将我的异常记录到svclog文件中,直到客户端将其发送给我进行调试。但我想删除app.config,只需使用C代码即可。

我搜索了很多,所以我们无法自动执行此操作,但这是一种很好的方法:

public static class myClass
{

    [XmlRoot("Exception"), System.Xml.Serialization.XmlType("Exception")]
    public class exception
    {
        public exception()
        {
            ExceptionType = "";
            appDomain = "";
            Message = "";
            StackTrace = "";
            ExceptionString = "";
            InnerException = null;
            HelpLink = "";
            TargetSite = "";
            Source = "";
        }

        internal exception(Exception ex)
        {
            ExceptionType = ex.GetType().ToString();
            appDomain = AppDomain.CurrentDomain.FriendlyName;
            Message = ex.Message;
            StackTrace = ex.StackTrace == null ? " - " : ex.StackTrace.ToString();
            HelpLink = ex.HelpLink == null ? " - " : ex.HelpLink.ToString();
            TargetSite = ex.TargetSite == null ? " - " : ex.TargetSite.ToString();
            Source = ex.Source == null ? " - " : ex.Source.ToString();
            ExceptionString = ex.ToString();
            if (ex.InnerException != null)
                InnerException = new exception(ex.InnerException);
        }

        [XmlElement("ExceptionType")]
        public string ExceptionType { get; set; }
        [XmlElement("AppDomain")]
        public string appDomain { get; set; }
        [XmlElement("Message")]
        public string Message { get; set; }
        [XmlElement("StackTrace")]
        public string StackTrace { get; set; }
        [XmlElement("ExceptionString")]
        public string ExceptionString { get; set; }
        [XmlElement("HelpLink")]
        public string HelpLink { get; set; }
        [XmlElement("TargetSite")]
        public string TargetSite { get; set; }
        [XmlElement("Source")]
        public string Source { get; set; }
        [XmlElement("InnerException")]
        public exception InnerException { get; set; }
    }

    private static System.Diagnostics.TraceSource TraceLogger { get; set; }

    static myClass()
    {
        var xml = new System.Diagnostics.XmlWriterTraceListener(System.IO.Directory.GetCurrentDirectory() + "\\ApplicationTrace.svclog");
        xml.TraceOutputOptions = System.Diagnostics.TraceOptions.Callstack | System.Diagnostics.TraceOptions.DateTime | System.Diagnostics.TraceOptions.LogicalOperationStack | System.Diagnostics.TraceOptions.ProcessId | System.Diagnostics.TraceOptions.ThreadId | System.Diagnostics.TraceOptions.Timestamp;

        TraceLogger = new System.Diagnostics.TraceSource("TraceLogger");
        TraceLogger.Switch.Level = System.Diagnostics.SourceLevels.All;
        TraceLogger.Listeners.Add(xml);

        TraceLogger.Flush();
        TraceLogger.Close();

    }

    private static void WriteException(Exception ex)
    {
        using (var writer = new StringWriter())
        {
            exception ee = new exception(ex);
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(ee.GetType());
            x.Serialize(writer, ee);
            var xmlEncodedList = writer.GetStringBuilder().ToString();
            XmlTextReader myXml = new XmlTextReader(new StringReader(xmlEncodedList));

            XPathDocument xDoc = new XPathDocument(myXml);
            XPathNavigator myNav = xDoc.CreateNavigator();
            TraceLogger.TraceData(System.Diagnostics.TraceEventType.Error, 0, myNav);
            TraceLogger.Flush();
        }
    }

    public static void HandleExceptions(Exception ex)
    {
       //do some thing then log
            WriteException(ex);
    }
}
应用程序中出现的每个错误都可以调用此方法,也可以使用application.DispatcherUnhandledException事件来捕获错误



我们创建一个可以用xmlSerialization进行序列化的异常类,然后我们有一个记录异常的静态跟踪源。

看看这篇文章:我不想以编程方式处理它,我想在程序中执行所有操作并删除app.configDoing“在程序中执行所有操作”就是以编程方式执行。如果您不想使用app.config文件,则必须在代码中以编程方式进行。没有第三种选择。我读过那个网站,我的理解是你创建了一个自定义类,他在app.config中使用了那个自定义类。。。所以他仍然使用app.config——他希望通过这种方式打开或关闭跟踪,他仍然有app.config,但他可以通过codeAh关闭它。我没听懂那部分。您可以在代码中或通过app.config执行很多操作,但这可能是无法实现的(至少不容易实现)。
public static class myClass
{

    [XmlRoot("Exception"), System.Xml.Serialization.XmlType("Exception")]
    public class exception
    {
        public exception()
        {
            ExceptionType = "";
            appDomain = "";
            Message = "";
            StackTrace = "";
            ExceptionString = "";
            InnerException = null;
            HelpLink = "";
            TargetSite = "";
            Source = "";
        }

        internal exception(Exception ex)
        {
            ExceptionType = ex.GetType().ToString();
            appDomain = AppDomain.CurrentDomain.FriendlyName;
            Message = ex.Message;
            StackTrace = ex.StackTrace == null ? " - " : ex.StackTrace.ToString();
            HelpLink = ex.HelpLink == null ? " - " : ex.HelpLink.ToString();
            TargetSite = ex.TargetSite == null ? " - " : ex.TargetSite.ToString();
            Source = ex.Source == null ? " - " : ex.Source.ToString();
            ExceptionString = ex.ToString();
            if (ex.InnerException != null)
                InnerException = new exception(ex.InnerException);
        }

        [XmlElement("ExceptionType")]
        public string ExceptionType { get; set; }
        [XmlElement("AppDomain")]
        public string appDomain { get; set; }
        [XmlElement("Message")]
        public string Message { get; set; }
        [XmlElement("StackTrace")]
        public string StackTrace { get; set; }
        [XmlElement("ExceptionString")]
        public string ExceptionString { get; set; }
        [XmlElement("HelpLink")]
        public string HelpLink { get; set; }
        [XmlElement("TargetSite")]
        public string TargetSite { get; set; }
        [XmlElement("Source")]
        public string Source { get; set; }
        [XmlElement("InnerException")]
        public exception InnerException { get; set; }
    }

    private static System.Diagnostics.TraceSource TraceLogger { get; set; }

    static myClass()
    {
        var xml = new System.Diagnostics.XmlWriterTraceListener(System.IO.Directory.GetCurrentDirectory() + "\\ApplicationTrace.svclog");
        xml.TraceOutputOptions = System.Diagnostics.TraceOptions.Callstack | System.Diagnostics.TraceOptions.DateTime | System.Diagnostics.TraceOptions.LogicalOperationStack | System.Diagnostics.TraceOptions.ProcessId | System.Diagnostics.TraceOptions.ThreadId | System.Diagnostics.TraceOptions.Timestamp;

        TraceLogger = new System.Diagnostics.TraceSource("TraceLogger");
        TraceLogger.Switch.Level = System.Diagnostics.SourceLevels.All;
        TraceLogger.Listeners.Add(xml);

        TraceLogger.Flush();
        TraceLogger.Close();

    }

    private static void WriteException(Exception ex)
    {
        using (var writer = new StringWriter())
        {
            exception ee = new exception(ex);
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(ee.GetType());
            x.Serialize(writer, ee);
            var xmlEncodedList = writer.GetStringBuilder().ToString();
            XmlTextReader myXml = new XmlTextReader(new StringReader(xmlEncodedList));

            XPathDocument xDoc = new XPathDocument(myXml);
            XPathNavigator myNav = xDoc.CreateNavigator();
            TraceLogger.TraceData(System.Diagnostics.TraceEventType.Error, 0, myNav);
            TraceLogger.Flush();
        }
    }

    public static void HandleExceptions(Exception ex)
    {
       //do some thing then log
            WriteException(ex);
    }
}