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");