Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用xml配置文件控制SOAP日志记录_C#_Web Services_Logging_Soap_System.diagnostics - Fatal编程技术网

C# 使用xml配置文件控制SOAP日志记录

C# 使用xml配置文件控制SOAP日志记录,c#,web-services,logging,soap,system.diagnostics,C#,Web Services,Logging,Soap,System.diagnostics,在我的web服务消息传递应用程序中,我通过将以下内容添加到我的app.config,启用了Soap日志记录: <system.diagnostics> <trace autoflush="true"/> <sources> <source name="System.Net"> <listeners> <add name="TraceFile"/>

在我的web服务消息传递应用程序中,我通过将以下内容添加到我的app.config,启用了Soap日志记录:

  <system.diagnostics>
    <trace autoflush="true"/>
    <sources>
      <source name="System.Net">
        <listeners>
          <add name="TraceFile"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="TraceFile" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Dev\EvryCardManagement2014\EvryCardManagement\trace.log"/>
    </sharedListeners>
    <switches>
      <add name="System.Net" value="Information"/>
      <!--add name="System.Net.Sockets" value="Information"/-->
    </switches>
  </system.diagnostics>
  <system.web>
    <webServices>
      <soapExtensionTypes>
        <add type="EvryCardManagement.SOAP.TraceExtension, EvryCardManagement" priority="1"/>
      </soapExtensionTypes>
    </webServices>
  </system.web>
这一切都很顺利

我的问题是,一旦我们上线,我们只希望在出现问题时记录soap消息,因此我需要能够通过在应用程序设置中设置值来控制是否启用日志记录


那么,我如何设置它,以便在我的应用程序属性中没有将该设置设置为true..设置,那么日志记录将被禁用?

这不是一个关于如何从配置文件读取值的问题吗?@JohnSaunders嗯,我可以这样做,但是我应该在哪里编写代码来禁用日志记录过程呢?没有办法关闭SoapExtension,因此您必须在扩展中编写该代码。只是不要登录.OTOH,更改web.config中的设置将导致AppDomain回收。这意味着将再次调用
GetInitializer
Initialize
方法。这将是检索日志设置的好地方。然后,您必须在每次
ProcessMessage
呼叫中强制执行这些命令。@johnsaunder如果您建议我可以接受它作为回答的话。。。
namespace EvryCardManagement.SOAP
{
    // Define a SOAP Extension that traces the SOAP request and SOAP 
    // response for the XML Web service method the SOAP extension is 
    // applied to. 

    public class TraceExtension : SoapExtension
        {
        Stream oldStream;
        Stream newStream;
        string filename;

        // Save the Stream representing the SOAP request or SOAP response into 
        // a local memory buffer. 
        public override Stream ChainStream(Stream stream)
            {
            oldStream = stream;
            newStream = new MemoryStream();
            return newStream;
            }

        // When the SOAP extension is accessed for the first time, the XML Web 
        // service method it is applied to is accessed to store the file 
        // name passed in, using the corresponding SoapExtensionAttribute.   
        public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
            {
            return ((TraceExtensionAttribute)attribute).Filename;
            }

        // The SOAP extension was configured to run using a configuration file 
        // instead of an attribute applied to a specific XML Web service 
        // method. 
        public override object GetInitializer(Type WebServiceType)
            {
            // Return a file name to log the trace information to, based on the 
            // type. 
                return global::EvryCardManagement.Properties.Settings.Default.SoapLog + WebServiceType.FullName + ".log";
            }

        // Receive the file name stored by GetInitializer and store it in a 
        // member variable for this specific instance. 
        public override void Initialize(object initializer)
            {
            filename = (string)initializer;
            }

        //  If the SoapMessageStage is such that the SoapRequest or 
        //  SoapResponse is still in the SOAP format to be sent or received, 
        //  save it out to a file. 
        public override void ProcessMessage(SoapMessage message)
            {
            switch (message.Stage)
                {
                case SoapMessageStage.BeforeSerialize:
                    break;
                case SoapMessageStage.AfterSerialize:
                    WriteOutput(message);
                    break;
                case SoapMessageStage.BeforeDeserialize:
                    WriteInput(message);
                    break;
                case SoapMessageStage.AfterDeserialize:
                    break;
                }
            }

        public void WriteOutput(SoapMessage message)
            {
            newStream.Position = 0;
            FileStream fs = new FileStream(filename, FileMode.Append,
                FileAccess.Write);
            StreamWriter w = new StreamWriter(fs);

            string soapString = (message is SoapServerMessage) ? "SoapResponse" : "SoapRequest";
            w.WriteLine("-----" + soapString + " at " + DateTime.Now);
            w.Flush();
            Copy(newStream, fs);
            w.Close();
            newStream.Position = 0;
            Copy(newStream, oldStream);
            }

        public void WriteInput(SoapMessage message)
            {
            Copy(oldStream, newStream);
            FileStream fs = new FileStream(filename, FileMode.Append,
                FileAccess.Write);
            StreamWriter w = new StreamWriter(fs);

            string soapString = (message is SoapServerMessage) ?
                "SoapRequest" : "SoapResponse";
            w.WriteLine("-----" + soapString +
                " at " + DateTime.Now);
            w.Flush();
            newStream.Position = 0;
            Copy(newStream, fs);
            w.Close();
            newStream.Position = 0;
            }

        void Copy(Stream from, Stream to)
            {
            TextReader reader = new StreamReader(from);
            TextWriter writer = new StreamWriter(to);
            writer.WriteLine(reader.ReadToEnd());
            writer.Flush();
            }
        }

    // Create a SoapExtensionAttribute for the SOAP Extension that can be 
    // applied to an XML Web service method.
    [AttributeUsage(AttributeTargets.Method)]
    public class TraceExtensionAttribute : SoapExtensionAttribute
        {

        private string filename = "C:\\Evry Card Management\\SOAP Logs\\Soaplog.txt";
        private int priority;

        public override Type ExtensionType
            {
            get { return typeof(TraceExtension); }
            }

        public override int Priority
            {
            get { return priority; }
            set { priority = value; }
            }

        public string Filename
            {
            get
                {
                return filename;
                }
            set
                {
                filename = value;
                }
            }
        }