【问题标题】:add application listener by code通过代码添加应用程序监听器
【发布时间】:2013-04-05 22:01:31
【问题描述】:

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

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.diagnostics>
    <sources>
      <source propagateActivity="true" name="System.ServiceModel" switchValue="Warning">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="NewListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging" switchValue="Warning,ActivityTracing" >
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="NewListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="Trace.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
          name="NewListener" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack">
        <filter type="" />
      </add>
    </sharedListeners>
  </system.diagnostics>

 </configuration>

此配置所做的是将所有发生的异常自动记录在一个文件中,我不需要为此编写代码。我正在寻找一种简单的方法来将我的异常记录到 svclog 文件中,直到客户端将其发送给我进行调试

【问题讨论】:

  • 我不想以编程方式处理它我想在程序中执行所有操作并删除 app.config
  • “全部在程序中”执行 以编程方式执行。 如果您不想使用 app.config 文件,则必须在代码中执行- 以编程方式。没有第三种选择。
  • 我读了那个网站,我知道你创建了一个自定义类,在 app.config 中他使用了那个自定义类......所以他仍然使用 app.config - 他想打开或关闭以这种方式跟踪他仍然有 app.config 但他可以通过代码将其关闭
  • 啊。我没有抓住那部分。您可以在代码中或通过 app.config 做很多事情 - 但这可能是无法实现的(至少不容易)。

标签: c# wcf configuration app-config


【解决方案1】:

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

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 进行序列化的异常类,然后我们有一个静态跟踪源来记录我们的异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-20
    • 2020-08-21
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多