Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to Design and implement .NET Log system

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to design and implement a .NET log system". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to design and implement a .NET log system.

Overall architecture diagram

Here I classify the days into tracking, BUG and error definitions and enumerate them as follows

The copy code is as follows:

/ / /

/ / Log level

/ / /

Public enum Loglevel

{

Track=1

Bug

Error

}

Consider the extensibility of the log module (database and file are supported here). Here the adapter pattern is used to complete this module. Here are the year-end benefits for everyone. Post some code.

Define an interface ILogTarget

The copy code is as follows:

Public interface ILogTarget

{

/ / /

/ / write tracking information

/ / /

/ / /

Void WriteTrack (string LogContent)

/ / /

/ / write BUG information

/ / /

/ / /

Void WriteBug (string LogContent)

/ / /

/ / write error message

/ / /

/ / /

Void WriteError (string LogContent)

}

The above interfaces of FileLog and DBLog classes are not affixed with specific reality.

The copy code is as follows:

/ / /

/ / File log implementation class

/ / /

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 ()

}

}

The copy code is 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 ()

}

}

The copy code is 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

The copy code is as follows:

SmartLog log = new SmartLog (new FileLog ())

Log.WriteTrack ("Hello word")

Thank you for your reading. the above is the content of "how to Design and implement .NET Log system". After the study of this article, I believe you have a deeper understanding of how to design and implement .NET Log system. Specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report