3

我有一个使用 https 协议的 wcf 服务。我使用证书进行客户端身份验证以及传输级别的安全性。

我想知道是否有办法记录幕后发生的握手细节。我目前正在记录客户端发送的证书的详细信息(使用自定义证书验证器)。但它仅用于客户端身份验证。

我想记录幕后发生的事情。我在几个地方看到他们使用 netmon 查看与握手相关的数据。有没有办法以某种格式记录数据,在 WCF 服务的情况下,它只是讲述发生的握手。

我使用 Windows 服务托管了我的 WCF 服务。

谢谢

4

2 回答 2

1

使用自定义跟踪器可能会为您提供很好的服务:

您的自定义跟踪器类:

using System.Diagnostics;

namespace MambaBase.Utils
{
    public class TraceLogger : TraceListener
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#if EXTENDED_DEBUG
        private string s(object payload)
        {
            try
            {
                return Newtonsoft.Json.JsonConvert.SerializeObject(payload, new Newtonsoft.Json.JsonSerializerSettings() { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore });
            }
            catch (Exception)
            {
                return "°°°";
            }
        }
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if (log.IsDebugEnabled) log.Debug("eventCache:" + s(eventCache) + " / source:" + s(source) + " / eventType:" + s(eventType) + " / Id:" + id + " / data:" + s(data));
            base.TraceData(eventCache, source, eventType, id, data);
        }

        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data)
        {
            if (log.IsDebugEnabled) log.Debug("eventCache:" + s(eventCache) + " / source:" + s(source) + " / eventType:" + s(eventType) + " / Id:" + id + " / data:" + s(data));
            base.TraceData(eventCache, source, eventType, id, data);
        }

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id)
        {
            if (log.IsDebugEnabled) log.Debug("eventCache:" + s(eventCache) + " / source:" + s(source) + " / eventType:" + s(eventType) + " / Id:" + id );
            base.TraceEvent(eventCache, source, eventType, id);
        }
        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
        {
            if (log.IsDebugEnabled) log.Debug("eventCache:" + s(eventCache) + " / source:" + s(source) + " / eventType:" + s(eventType) + " / Id:" + id + " / format:" + format + " / args:" + s(args));
            base.TraceEvent(eventCache, source, eventType, id, format, args);
        }

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
        {
            if (log.IsDebugEnabled) log.Debug("eventCache:" + s(eventCache) + " / source:" + s(source) + " / eventType:" + s(eventType) + " / Id:" + id + " / message:" + message);
            base.TraceEvent(eventCache, source, eventType, id, message);
        }

        public override void TraceTransfer(TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId)
        {
            if (log.IsDebugEnabled) log.Debug("eventCache:" + s(eventCache) + " / source:" + s(source) + " / Id:" + id + " / message:" + message + " / relatedActivityId:" + relatedActivityId.ToString());
            base.TraceTransfer(eventCache, source, id, message, relatedActivityId);
        }
        public override void Fail(string message)
        {
            if (log.IsDebugEnabled) log.Debug("Fail - message:" + message);
            base.Fail(message);
        }

        public override void WriteLine(object o)
        {
            if (log.IsDebugEnabled) log.Debug("Trace:" + s(o));
            base.WriteLine(o);
        }
        public override void Write(object o, string category)
        {
            if (log.IsDebugEnabled) log.Debug("Trace:" + s(o) + " / category:" + category);
            base.Write(o, category);
        }
        public override void Write(string message, string category)
        {
            if (log.IsDebugEnabled) log.Debug("Trace:" + message + " / category:" + category);
            base.Write(message, category);
        }

        public override void WriteLine(object o, string category)
        {
            if (log.IsDebugEnabled) log.Debug("Trace:" + s(o) + " / category:" + category);
            base.WriteLine(o, category);
        }

        public override void Write(object o)
        {
            if (log.IsDebugEnabled) log.Debug("Trace:" + s(o) );
            base.Write(o);
        }

        public override void WriteLine(string message, string category)
        {
            if (log.IsDebugEnabled) log.Debug("Trace:" + message + " / category:" + category);
            base.WriteLine(message, category);
        }

#endif
        public override void Write(string message)
        {
#if EXTENDED_DEBUG
            if (log.IsDebugEnabled) log.Debug("Trace:" + message);
            Debug.Write(message);
#endif
        }
        public override void WriteLine(string message)
        {
            if (log.IsDebugEnabled) log.Debug("Trace ==> " + message);
            Debug.WriteLine(message);
        }
    }
}

在应用程序的 web.config 或 app.config 中:

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
        <listeners><add name="CustomTracer"/></listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners><add name="CustomTracer"/></listeners>
      </source>
      <source name="System.Net">
        <listeners><add name="CustomTracer"/></listeners>
      </source>
      <source name="System.Net.Sockets">
        <listeners><add name="CustomTracer"/></listeners>
      </source>
      <source name="System.Net.Cache">
        <listeners><add name="CustomTracer"/></listeners>
      </source>
      <source name="System.Net.Security">
        <listeners><add name="CustomTracer"/></listeners>
      </source>
      <source name="System.Security">
        <listeners><add name="CustomTracer"/></listeners>
      </source>
    </sources>
    <switches>
      <add name="System.ServiceModel" value="Verbose"/>
      <add name="System.ServiceModel.MessageLogging" value="Verbose"/>
      <add name="System.Net" value="Verbose"/>
      <add name="System.Net.Sockets" value="Verbose"/>
      <add name="System.Net.Cache" value="Verbose"/>
      <add name="System.Security" value="Verbose"/>
      <add name="System.Net.Security" value="Verbose"/>
    </switches>
    <sharedListeners>
      <add name="CustomTracer" type="MambaBase.Utils.TraceLogger, MambaBase" />
    </sharedListeners>
    <trace autoflush="true"/>
  </system.diagnostics>

  <system.serviceModel>
    <diagnostics performanceCounters="All" wmiProviderEnabled="true">
      <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" maxMessagesToLog="100000" />
    </diagnostics>
  </system.serviceModel>

不用说,这将产生大量的日志记录,如果您激活 EXTENDED_DEBUG 则更是如此。

并且日志将包含敏感数据,例如密码。

于 2019-03-13T12:39:51.763 回答
0

最好的方法是使用图形工具:查看类似的路径:C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools

找到 SvcConfigEditor.exe。

使用它打开 web.config,您现在可以轻松添加诊断信息,包括您所要求的内容。

于 2019-03-17T15:56:00.077 回答