C # custom exception classes

Source: Internet
Author: User
UsingSystem; usingSystem. Web; usingSystem. IO; Summary of namespaceCommon {summaryExceptionLog. SummarypublicclassExceptionLog: ApplicationException {publicExceptionLog (stringmessage): base (message) {Init (); Log

Using System; using System. Web; using System. IO; summary of namespace Common {// summary // ExceptionLog. /// Summary public class ExceptionLog: ApplicationException {public ExceptionLog (string message): base (message) {Init (); Log

Using System;
Using System. Web;
Using System. IO;

Namespace Common
{
///


/// Summary of predictionlog.
///
Public class ExceptionLog: ApplicationException
{
Public ExceptionLog (string message)
: Base (message)
{
Init ();
Log ();
}

Public ExceptionLog (Exception inner)
{
Ex = inner;
Init ();
Log ();
}

Public Exception ex = null;

Public override string Message
{
Get
{
String msg = base. Message;
If (ex! = Null)
{
Msg = ex. Message;
}
Return msg;
}
}

String username = string. Empty;
Public string UserName
{
Get
{
Return username;
}
Set
{
Username = value;
}
}

String userAgent = string. Empty;
Public string UserAgent
{
Get
{
Return userAgent;
}
Set
{
UserAgent = value;
}
}

String ipAddress = string. Empty;
Public string IPAddress
{
Get
{
Return ipAddress;
}
Set
{
IpAddress = value;
}
}

String httpReferrer = string. Empty;
Public string HttpReferrer
{
Get
{
Return httpReferrer;
}
Set
{
HttpReferrer = value;
}
}

String httpVerb = string. Empty;
Public string HttpVerb
{
Get
{
Return httpVerb;
}
Set
{
HttpVerb = value;
}
}

String httpPathAndQuery = string. Empty;
Public string HttpPathAndQuery
{
Get
{
Return httpPathAndQuery;
}
Set
{
HttpPathAndQuery = value;
}
}

DateTime dateCreated;
Public DateTime DateCreated
{
Get
{
Return dateCreated;
}
Set
{
DateCreated = value;
}
}

Void Init ()
{
DateCreated = DateTime. Now;
If (HttpContext. Current. Request. UrlReferrer! = Null)
HttpReferrer = HttpContext. Current. Request. UrlReferrer. ToString ();

If (HttpContext. Current. Request. UserAgent! = Null)
UserAgent = HttpContext. Current. Request. UserAgent;

If (HttpContext. Current. Request. UserHostAddress! = Null)
IpAddress = HttpContext. Current. Request. UserHostAddress;

Try
{
If (HttpContext. Current. Request! = Null
& HttpContext. Current. Request. RequestType! = Null)
HttpVerb = HttpContext. Current. Request. RequestType;
}
Catch (Exception ex)
{
System. Diagnostics. Debug. WriteLine (ex. ToString ());
}

If (HttpContext. Current. Request! = Null
& HttpContext. Current. Request. Url! = Null
& HttpContext. Current. Request. Url. PathAndQuery! = Null)
HttpPathAndQuery = HttpContext. Current. Request. Url. PathAndQuery;

If (HttpContext. Current. Request! = Null
& HttpContext. Current. Request. UrlReferrer! = Null
& HttpContext. Current. Request. Url. PathAndQuery! = Null)
HttpReferrer = HttpContext. Current. Request. UrlReferrer. ToString ();
}

Public void Log ()
{
String LogName = DateTime. Now. tow.datestring () + ". txt ";
String FilePath = HttpContext. Current. Request. PhysicalApplicationPath + "// Log //" + LogName;
If (! File. Exists (FilePath ))
{
Using (StreamWriter sw = File. CreateText (FilePath ))
{

Sw. WriteLine ("Fields: Value ");
Sw. WriteLine ();
}
}
Using (StreamWriter sw = File. AppendText (FilePath ))
{
Sw. writeLine ("========================================== = ");
Sw. WriteLine ("DateTime:" + this. DateCreated );
Sw. WriteLine ("Message:" + this. Message );
Sw. WriteLine ("IPAddress:" + this. IPAddress );
Sw. WriteLine ("HttpReferrer:" + this. HttpReferrer );
Sw. WriteLine ("HttpVerb:" + this. HttpVerb );
Sw. WriteLine ("HttpPathAndQuery:" + this. HttpPathAndQuery );
Sw. WriteLine ("UserName:" + this. UserName );
Sw. WriteLine ("UserAgent:" + this. UserAgent );
Sw. WriteLine ();
}

}
}
}

// Page reference

Try

{

}

Catch (Exception ex)
{
Throw new Common. ExceptionLog (ex );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.