Miguelangelgonzalez

Sin viento, el pasto no se mueve. Sin software, el hardware es inútil.

Log4net con Exchange

Publicado por Miguel Gonzalez en 22 Diciembre 2008

Para realizar esto primero tenemos que crear una clase personalizada para tal fin. Para esto vamos a heredar de “log4net.Appender.SmtpAppender” para agregar esta nueva caracteristica. De esta forma nace la clase “MyApp.ExchangeAppender” que usaremos siempre que queramos enviar nuestro logs atraves de una cuenta exchange. Aqui tiene la clase que yo utilizo para esto.

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace MyApp
{
public class ExchangeAppender : log4net.Appender.SmtpAppender
{
protected override void SendEmail(string messageBody)
{
try
{
string sUri;
sUri = “https://miservidor/exchange/” + Username;
sUri = sUri + “/%23%23DavMailSubmissionURI%23%23″;

System.Uri myUri = new System.Uri(sUri);
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);
string sQuery;
sQuery = “From: ” + From + “n” +
“To: ” + To + “n” +
//”CC: ” + cc + “n” +
“Subject: ” + Subject + “n” +
“Date: ” + DateTime.Now.ToString() + “n” +
“X-Mailer: My DAV mailer” + “n” +
“MIME-Version: 1.0″ + “n” +
“Content-Type: text/plain;” + “n” +
“Charset = “iso-8859-1″” + “n” +
“Content-Transfer-Encoding: 7bit” + “n” + “n” +
messageBody;
// Set the credentials.
// TODO: Replace with the appropriate user credential.
NetworkCredential myCred = new NetworkCredential(Username, Password);
CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(myUri, “Basic”, myCred);
HttpWRequest.Credentials = myCredentialCache;
// Set the headers.
HttpWRequest.Headers.Set(“Translate”, “f”);
HttpWRequest.ContentType = “message/rfc822″;
HttpWRequest.ContentLength = sQuery.Length;
//Set the request timeout to 5 minutes.
HttpWRequest.Timeout = 300000;
// Set the request method.
HttpWRequest.Method = “PUT”;
// Store the data in a byte array.
byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery);
HttpWRequest.ContentLength = ByteQuery.Length;
Stream QueryStream = HttpWRequest.GetRequestStream();
// write the data to be posted to the Request Stream
QueryStream.Write(ByteQuery, 0, ByteQuery.Length);
QueryStream.Close();
// Send the request and get the response.
HttpWebResponse HttpWResponse =(HttpWebResponse)HttpWRequest.GetResponse();
// Get the Status code.
int iStatCode = (int)HttpWResponse.StatusCode;
string sStatus = iStatCode.ToString();
//log.DebugFormat(“Status Code: {0}”, sStatus);
// Get the request headers.
string sReqHeaders = HttpWRequest.Headers.ToString();
//log.Debug(sReqHeaders);
// Read the response stream.
Stream strm = HttpWResponse.GetResponseStream();
StreamReader sr = new StreamReader(strm);
string sText = sr.ReadToEnd();
// Close the stream.
strm.Close();
// Clean up.
myCred = null;
myCredentialCache = null;
HttpWRequest = null;
HttpWResponse = null;
QueryStream = null;
strm = null;
sr = null;
}
catch (Exception e)
{throw new Exception(e.Message);}
}
}
}

Y aqui tienen la configuracion correspondiente:

<appender name=”ExchangeAppender” type=”MyApp.ExchangeAppender”>
<to value=”xxx@dominio.com” />
<from value=”yyy@dominio.com” />
<subject value=”Asunto” />
<bufferSize value=”1″ />
<port value=”587″ />
<lossy value=”false” />
<authentication value=”Basic” />
<userName value=”usuario_exchange” />
<password value=”clave_exchange” />
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%newline%date [%thread] %-5level %logger [%property{NDC}] – %message%newline%newline%newline” />
</layout>
<filter type=”log4net.Filter.LevelRangeFilter”>
<levelMin value=”FATAL”/>
<levelMax value=”FATAL”/>
</filter>

Escribe un comentario

XHTML: Puedes usar estas etiquetas: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>