Author: bodewig
Date: Tue Mar 31 13:47:42 2015
New Revision: 1670341

URL: http://svn.apache.org/r1670341
Log:
LOG4NET-172 add WebServiceAppender to examples

Added:
    logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/
    
logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/AssemblyInfo.cs
   (with props)
    
logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/WebServiceAppender.cs
   (with props)
Modified:
    logging/log4net/trunk/examples.build

Modified: logging/log4net/trunk/examples.build
URL: 
http://svn.apache.org/viewvc/logging/log4net/trunk/examples.build?rev=1670341&r1=1670340&r2=1670341&view=diff
==============================================================================
--- logging/log4net/trunk/examples.build (original)
+++ logging/log4net/trunk/examples.build Tue Mar 31 13:47:42 2015
@@ -152,6 +152,32 @@ limitations under the License.
           <include name="System.Configuration.Install.dll" />
         </references>
       </csc>
+      <if test="${current.build.has.systemweb}">
+        <csc nostdlib="true" noconfig="true"
+             warnaserror="false"
+             target="library"
+             debug="true"
+             define="${current.build.defines.csc}"
+             output="${current.bin.dir}/WebServiceAppender.dll"
+             doc="${current.bin.dir}/WebServiceAppender.xml">
+          <sources basedir="${log4net.basedir}">
+            <include 
name="src/examples/cs/Appenders/WebServiceAppender/**/*.cs" />
+            <include name="src/log4net/AssemblyVersionInfo.cs"/>
+          </sources>
+          <resources refid="nl-files"/>
+          <references>
+            <include name="mscorlib.dll"/>
+            <include name="System.dll" />
+            <include name="System.Web.dll" />
+            <include name="System.Web.Services.dll" />
+            <include name="System.Xml.dll" />
+            <include name="System.Configuration.dll" 
if="${current.build.has.systemconfiguration}"/>
+            <include 
name="${log4net.basedir}/lib/main/${framework::get-family(framework::get-target-framework())}/${framework::get-version(framework::get-target-framework())}/*.dll"
 />
+            <include name="${log4net.basedir}/lib/main/*.dll" />
+            <include name="${log4net.main.output.dir}/log4net-1.3.dll" 
frompath="false"/>
+          </references>
+        </csc>
+      </if>
       <csc nostdlib="true" noconfig="true"
            warnaserror="false"
            target="library"

Added: 
logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/AssemblyInfo.cs
URL: 
http://svn.apache.org/viewvc/logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/AssemblyInfo.cs?rev=1670341&view=auto
==============================================================================
--- 
logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/AssemblyInfo.cs
 (added)
+++ 
logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/AssemblyInfo.cs
 Tue Mar 31 13:47:42 2015
@@ -0,0 +1,67 @@
+#region Apache License
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more 
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership. 
+// The ASF licenses this file to you under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with 
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+#endregion
+
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+//
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+//
+[assembly: AssemblyTitle("log4net.Appender.WebServiceAppender")]
+[assembly: AssemblyDescription("The Apache Software Foundation log4net Logging 
Framework - WebServiceAppender")]
+
+#if DEBUG
+[assembly: AssemblyConfiguration("Debug")]
+#else
+[assembly: AssemblyConfiguration("Retail")]
+#endif
+
+[assembly: AssemblyProduct("log4net")]
+[assembly: AssemblyCulture("")]                
+
+//
+// In order to sign your assembly you must specify a key to use. Refer to the 
+// Microsoft .NET Framework documentation for more information on assembly 
signing.
+//
+// Use the attributes below to control which key is used for signing. 
+//
+// Notes: 
+//   (*) If no key is specified, the assembly is not signed.
+//   (*) KeyName refers to a key that has been installed in the Crypto Service
+//       Provider (CSP) on your machine. KeyFile refers to a file which 
contains
+//       a key.
+//   (*) If the KeyFile and the KeyName values are both specified, the 
+//       following processing occurs:
+//       (1) If the KeyName can be found in the CSP, that key is used.
+//       (2) If the KeyName does not exist and the KeyFile does exist, the key 
+//           in the KeyFile is installed into the CSP and used.
+//   (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) 
utility.
+//       When specifying the KeyFile, the location of the KeyFile should be
+//       relative to the project output directory which is
+//       %Project Directory%\obj\<configuration>. For example, if your KeyFile 
is
+//       located in the project directory, you would specify the 
AssemblyKeyFile 
+//       attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
+//   (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
+//       documentation for more information on this.
+//
+[assembly: AssemblyDelaySign(false)]
+[assembly: AssemblyKeyFile("")]
+[assembly: AssemblyKeyName("")]

Propchange: 
logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/AssemblyInfo.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/WebServiceAppender.cs
URL: 
http://svn.apache.org/viewvc/logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/WebServiceAppender.cs?rev=1670341&view=auto
==============================================================================
--- 
logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/WebServiceAppender.cs
 (added)
+++ 
logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/WebServiceAppender.cs
 Tue Mar 31 13:47:42 2015
@@ -0,0 +1,401 @@
+using System;
+using System.CodeDom;
+using System.CodeDom.Compiler;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Text;
+using System.Web.Services.Description;
+
+using log4net.Core;
+using log4net.Layout;
+using log4net.Util;
+
+namespace log4net.Appender
+{
+    /// <summary>
+    /// Appender that logs to a web service.
+    /// </summary>
+    /// <remarks>
+    /// <para>
+    /// <see cref="WebServiceAppender"/> appends logging events to a web 
service. 
+    /// The appender can be configured to specify the web service's URL by 
setting 
+    /// the <see cref="serviceUrl"/> property. 
+    /// The service name can be specified by setting the <see 
cref="serviceName"/>
+    /// property.
+    /// The method name can be specified by setting the <see 
cref="methodName"/>
+    /// property. 
+    /// </para>
+    /// <para>
+    /// The web service can take any number of parameters.  All parameters are 
accepted
+    /// as strings. Parameters are added using the <see cref="AddParameter"/>
+    /// method. This adds a single <see cref="WebServiceAppenderParameter"/> 
to the
+    /// list of parameters. The <see cref="WebServiceAppenderParameter"/>
+    /// type may be subclassed if required to provide specific
+    /// functionality. The <see cref="WebServiceAppenderParameter"/> specifies
+    /// the parameter name and how the value should be generated using a <see 
cref="ILayout"/>.
+    /// </para>
+    /// </remarks>
+    /// <example>
+    /// An example configuration to log to a simple web service:
+    /// <code lang="XML" escaped="true">
+    /// <appender name="WebServiceAppender" 
type="log4net.Appender.WebServiceAppender" >
+    ///   <serviceUrl value="http://example.com/LogService.asmx"; />
+    ///          <serviceName value="LogService" />
+       ///       <methodName value="LogError" />
+    ///   <parameter>
+    ///     <parameterName value="Date" />
+    ///     <layout type="log4net.Layout.RawTimeStampLayout"/>
+    ///   </parameter>
+    ///   <parameter>
+    ///     <parameterName value="Level" />
+    ///     <layout type="log4net.Layout.PatternLayout" value="%level" />
+    ///   </parameter>
+    ///   <parameter>
+    ///     <parameterName value="Message" />
+    ///     <layout type="log4net.Layout.PatternLayout" value="%message" />
+    ///   </parameter>
+    ///   <parameter>
+       ///             <parameterName value="Exception"/>
+       ///             <layout type="log4net.Layout.ExceptionLayout"/>
+       ///       </parameter>
+    /// </appender>
+    /// </code>
+    /// </example>
+    /// <author>Brandon Wood</author>
+    public class WebServiceAppender : AppenderSkeleton
+    {
+        #region Public Instance Constructors
+
+        /// <summary> 
+        /// Initializes a new instance of the <see cref="WebServiceAppender" 
/> class.
+        /// </summary>
+        /// <remarks>
+        /// Public default constructor to initialize a new instance of this 
class.
+        /// </remarks>
+        public WebServiceAppender()
+        {
+            this.m_parameters = new List<WebServiceAppenderParameter>();
+        }
+
+        #endregion // Public Instance Constructors
+
+        #region Public Instance Properties
+
+        /// <summary>
+        /// The web service URL.
+        /// </summary>
+        public string ServiceUrl
+        {
+            get { return this.m_serviceUrl; }
+            set { this.m_serviceUrl = value; }
+        }
+
+        /// <summary>
+        /// The web service name.
+        /// </summary>
+        public string ServiceName
+        {
+            get { return this.m_serviceName; }
+            set { this.m_serviceName = value; }
+        }
+
+        /// <summary>
+        /// The web service method name.
+        /// </summary>
+        public string MethodName
+        {
+            get { return this.m_methodName; }
+            set { this.m_methodName = value; }
+        }
+
+        #endregion // Public Instance Properties
+
+        #region Override implementation of AppenderSkeleton
+
+        /// <summary>
+        /// Sends the logging event to a web service.
+        /// </summary>
+        /// <param name="loggingEvent">The event to log.</param>
+        /// <remarks>
+        /// <para>
+        /// Sends the logging event to a web service.
+        /// </para>
+        /// </remarks>
+        override protected void Append(LoggingEvent loggingEvent)
+        {
+            InitializeWebServiceConnection();
+
+            if (this.m_webServiceParameters != null && 
this.m_webServiceParameters.Length > 0)
+            {
+                string[] args = new string[this.m_webServiceParameters.Length];
+                int paramIndex = 0;
+
+                // use paramInfo array to supply web service parameter values 
in the correct order
+                foreach (ParameterInfo paramInfo in 
this.m_webServiceParameters)
+                {
+                    // default all parameters to empty string
+                    args[paramIndex] = string.Empty;
+
+                    foreach (WebServiceAppenderParameter param in 
this.m_parameters)
+                    {
+                        if (paramInfo.Name.ToUpper() == 
param.ParameterName.ToUpper())
+                        {
+                            args[paramIndex] = param.FormatValue(loggingEvent);
+                            break;
+                        }
+                    }
+
+                    paramIndex++;
+                }
+
+                this.m_webMethodInfo.Invoke(this.m_webServiceInstance, args);
+            }
+        }
+
+        #endregion // Override implementation of AppenderSkeleton
+
+        #region Public Instance Methods
+
+        /// <summary>
+        /// Adds a parameter to send to the web service.
+        /// </summary>
+        /// <param name="parameter">The parameter to add to the web service 
method call.</param>
+        /// <remarks>
+        /// <para>
+        /// Adds a parameter to send to the web service method call.
+        /// </para>
+        /// </remarks>
+        public void AddParameter(WebServiceAppenderParameter parameter)
+        {
+            m_parameters.Add(parameter);
+        }        
+
+        #endregion // Public Instance Methods
+
+        #region Private Instance Methods
+
+        /// <summary>
+        /// Connects to the web service.
+        /// </summary>
+        private void InitializeWebServiceConnection()
+        {
+            ServiceDescription description;
+
+            try
+            {
+                // connect to the web service
+                System.Net.WebClient client = new System.Net.WebClient();
+                System.IO.Stream stream = client.OpenRead(this.m_serviceUrl + 
"?wsdl");
+
+                // read the WSDL file describing the service.
+                description = ServiceDescription.Read(stream);
+            }
+            catch (Exception ex)
+            {
+                ErrorHandler.Error("Error reading the web service 
definition.", ex);
+                return;
+            }
+
+            // Initialize a service description importer.
+            ServiceDescriptionImporter importer = new 
ServiceDescriptionImporter();
+            importer.ProtocolName = "Soap12"; // Use SOAP 1.2.
+            importer.AddServiceDescription(description, null, null);
+            // Generate a proxy client.
+            importer.Style = ServiceDescriptionImportStyle.Client;
+            // Generate properties to represent primitive values.
+            importer.CodeGenerationOptions = 
System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;
+
+            // Initialize a Code-DOM tree into which we will import the 
service.
+            CodeNamespace codeNamespace = new CodeNamespace();
+            CodeCompileUnit compileUnit = new CodeCompileUnit();
+            compileUnit.Namespaces.Add(codeNamespace);
+
+            // Import the service into the Code-DOM tree. This creates proxy 
code that uses the service.
+            ServiceDescriptionImportWarnings warning = 
importer.Import(codeNamespace, compileUnit);
+
+            if (warning != 0)
+            {
+                ErrorHandler.Error("Error importing the web service 
definition.");
+                return;
+            }
+
+            // Generate the proxy code
+            CodeDomProvider domProvider = 
CodeDomProvider.CreateProvider("CSharp");
+
+            // Compile the assembly proxy with the appropriate references
+            string[] assemblyReferences = new string[5] { "System.dll", 
"System.Web.Services.dll", "System.Web.dll", "System.Xml.dll", 
"System.Data.dll" };
+            CompilerParameters compilerParams = new 
CompilerParameters(assemblyReferences);
+            CompilerResults compilerResults = 
domProvider.CompileAssemblyFromDom(compilerParams, compileUnit);
+
+            if (compilerResults.Errors.Count > 0)
+            {
+                StringBuilder errorMessages = new StringBuilder();
+                foreach (CompilerError error in compilerResults.Errors)
+                {
+                    errorMessages.AppendFormat("{0}\n", error.ErrorText);
+                }
+
+                ErrorHandler.Error("Error compiling web service:\n" + 
errorMessages.ToString());
+                return;
+            }
+
+            try
+            {
+                // get instance of web service
+                m_webServiceInstance = 
compilerResults.CompiledAssembly.CreateInstance(this.m_serviceName);
+                m_webMethodInfo = 
m_webServiceInstance.GetType().GetMethod(this.m_methodName);
+                this.m_webServiceParameters = m_webMethodInfo.GetParameters();
+            }
+            catch (Exception ex)
+            {
+                ErrorHandler.Error("Error creating web service instance.", ex);
+                return;
+            }
+        }
+
+        #endregion // Private Instance Methods
+
+        #region Protected Instance Fields
+
+        /// <summary>
+        /// The list of <see cref="WebServiceAppenderParameter"/> objects.
+        /// </summary>
+        /// <remarks>
+        /// <para>
+        /// The list of <see cref="WebServiceAppenderParameter"/> objects.
+        /// </para>
+        /// </remarks>
+        protected List<WebServiceAppenderParameter> m_parameters;
+
+        /// <summary>
+        /// <see cref="System.Reflection.ParameterInfo"/> array containing 
details of the web service parameters.
+        /// </summary>
+        protected ParameterInfo[] m_webServiceParameters;
+
+        /// <summary>
+        /// Object containing the web service instance.
+        /// </summary>
+        protected object m_webServiceInstance;
+
+        /// <summary>
+        /// <see cref="System.Reflection.MethodInfo"/> containing details of 
the web service method.
+        /// </summary>
+        protected MethodInfo m_webMethodInfo;
+
+        #endregion // Protected Instance Fields
+
+        #region Private Instance Fields
+
+        /// <summary>
+        /// Web service URL.
+        /// </summary>
+        private string m_serviceUrl;
+
+        /// <summary>
+        /// Web service name.
+        /// </summary>
+        private string m_serviceName;
+
+        /// <summary>
+        /// Web service method name.
+        /// </summary>
+        private string m_methodName;
+
+        #endregion // Private Instance Fields
+    }
+
+    public class WebServiceAppenderParameter
+    {
+        #region Public Instance Methods
+
+        /// <summary>
+               /// Renders the logging event and set the parameter value in 
the command.
+               /// </summary>
+               /// <param name="command">The command containing the 
parameter.</param>
+               /// <param name="loggingEvent">The event to be rendered.</param>
+               /// <remarks>
+               /// <para>
+               /// Renders the logging event using this parameters layout
+               /// object. Sets the value of the parameter on the command 
object.
+               /// </para>
+               /// </remarks>
+               virtual public string FormatValue(LoggingEvent loggingEvent)
+               {
+                       // Format the value
+                       object formattedValue = Layout.Format(loggingEvent);
+
+                       // If the value is null then return an empty string
+                       if (formattedValue == null)
+                       {
+                               formattedValue = string.Empty;
+                       }
+
+                       return formattedValue.ToString();
+               }
+
+               #endregion // Public Instance Methods
+
+        #region Public Instance Properties
+
+        /// <summary>
+        /// Gets or sets the name of this parameter.
+        /// </summary>
+        /// <value>
+        /// The name of this parameter.
+        /// </value>
+        /// <remarks>
+        /// <para>
+        /// The name of this parameter. The parameter name
+        /// must match up to a named parameter of the web service method.
+        /// </para>
+        /// </remarks>
+        public string ParameterName
+        {
+            get { return m_parameterName; }
+            set { m_parameterName = value; }
+        }
+
+        /// <summary>
+               /// Gets or sets the <see cref="IRawLayout"/> to use to 
+               /// render the logging event into an object for this 
+               /// parameter.
+               /// </summary>
+               /// <value>
+               /// The <see cref="IRawLayout"/> used to render the
+               /// logging event into an object for this parameter.
+               /// </value>
+               /// <remarks>
+               /// <para>
+               /// The <see cref="IRawLayout"/> that renders the value for this
+               /// parameter.
+               /// </para>
+               /// <para>
+               /// The <see cref="RawLayoutConverter"/> can be used to adapt
+               /// any <see cref="ILayout"/> into a <see cref="IRawLayout"/>
+               /// for use in the property.
+               /// </para>
+               /// </remarks>
+               public IRawLayout Layout
+               {
+                       get { return m_layout; }
+                       set { m_layout = value; }
+               }
+
+               #endregion // Public Instance Properties
+
+        #region Private Instance Fields
+
+        /// <summary>
+        /// The name of this parameter.
+        /// </summary>
+        private string m_parameterName;
+
+        /// <summary>
+        /// The <see cref="IRawLayout"/> to use to render the
+        /// logging event into an object for this parameter.
+        /// </summary>
+        private IRawLayout m_layout;
+
+        #endregion // Private Instance Fields
+    }
+}
\ No newline at end of file

Propchange: 
logging/log4net/trunk/src/examples/cs/Appenders/WebServiceAppender/WebServiceAppender.cs
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to