. NET Log system design ideas and implementation code _ practical skills

Source: Internet
Author: User

The log is obviously a very important means of helping people navigate to the problem. Originally want to use the Nlog to do the system of the log tool, Ah hurt, a pervert must say this has a lot of uncontrollable factors, here I tell you how I realize the Log module, welcome to Pat Bricks

Overall architecture diagram

• Here I divide the days into traces, bugs, and bugs. 3 definitions are enumerated as follows

Copy Code code as follows:

<summary>
Log level
</summary>
public enum LogLevel
{
Track=1,
Bugs,
Error
}

• Consider the scalability of the log module here (2 ways to support databases and files) Here, use adapter mode to complete this module. Here is the year-end benefits for everyone. Sticking point Code
Define an interface Ilogtarget
Copy Code code as follows:

public interface Ilogtarget
{
<summary>
Write Trace Information
</summary>
<param name= "Logcontent" ></param>
void Writetrack (string logcontent);

<summary>
Write Bug Information
</summary>
<param name= "Logcontent" ></param>
void Writebug (string logcontent);

<summary>
Write error message
</summary>
<param name= "Logcontent" ></param>
void Writeerror (string logcontent);

}



FileLog, and DBLog 2 classes to implement the above interface this is not a concrete reality.
Copy Code code as follows:

<summary>
File Log Implementation class
</summary>
public class Filelog:ilogtarget
{
public void Writetrack (string logcontent)
{
throw new NotImplementedException ();
}

public void Writebug (string logcontent)
{
throw new NotImplementedException ();
}

public void Writeerror (string logcontent)
{
throw new NotImplementedException ();
}
}


Copy Code code as follows:

public class Dblog:ilogtarget
{
public void Writetrack (string logcontent)
{
throw new NotImplementedException ();
}

public void Writebug (string logcontent)
{
throw new NotImplementedException ();
}

public void Writeerror (string logcontent)
{
throw new NotImplementedException ();
}
}


Copy Code code as follows:

public class Smartlog
{
Private Ilogtarget _adaptee;

Public Smartlog (Ilogtarget tragent)
{
This._adaptee = tragent;
}
public void Writetrack (string logcontent)
{
_adaptee. Writetrack (logcontent);
}

public void Writebug (string logcontent)
{
_adaptee. Writebug (logcontent);
}

public void Writeerror (string logcontent)
{
_adaptee. Writeerror (logcontent);
}
}


• Calling mode
Copy Code code as follows:

Smartlog Log =new Smartlog (new FileLog ());

Log. Writetrack ("Hello word");

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.