The so-called Application Block is the same as a wooden module, making it into a application. Of course, this is an ideal state, which is generally less than 100% of the items that can be shaped. That Microsoft did such a small building block for us to use it after you have been configured in your own procedure. In other words, Application Block is some components that are highly reusable.
The exception management application block is the simple point it is to do, which is the simplest processing of exceptions, and it is very strong as an Application Block. What is it doing? I will not explain, because it has been very detailed, and the same way, how to expand it is well introduced in the document, and some examples, interested friends can download on Microsoft's website. (Http://download.microsoft.com/). Simply put to configure some profiles, then call ExceptionManager.publish (Exception EX) in your catch, you can publish an exception, depending on your configuration file, a Publisher will abnormally Turn off according to their methods (records).
Here I will mention a bug in the example, and how to improve the ExceptionPublisher in the example and ExceptionXmlpublisher from others. This example is written to the file in the example there is such a code:
// Write the entry to the log file. Using (FileStream fs = File.Open (m_LogName, FileMode.Create, FileAccess.ReadWrite)) {using (StreamWriter sw = new StreamWriter (fs)) {sw.Write (strInfo.ToString ());}}
The problem is obvious. If you use filemode.create, you will not be written in the next time you need a record. This is definitely a bug as an iexceptionpublisher. Fortunately, we have source code, just change this thread's ExceptionPublisher, we can use it, and we have to join a necessary feature, that is, we can't make the log file infinite growth, we must Set the boundary, let it automatically delete the previous log and build a new come out when reaching the boundary. So we have the following code:
Using system; using system.io; using system.text; using microsoft.ApplicationBlocks.ExceptionManagement;
namespace Microsoft.ApplicationBlocks.ExceptionManagement {///
// Provide implementation of the IPublishException interface // This contains the single Publish method. Void IExceptionPublisher.Publish (Exception exception, NameValueCollection additionalInfo, NameValueCollection configSettings) {// Load Config values if they are provided. If (configSettings! = Null) { if (configSettings [ "fileName"] = null && configSettings [ "fileName"] Length> 0!.) {m_LogName = configSettings [ "fileName"];}! if (configSettings [ "maxFileSize"] = null && configSettings [ "maxFileSize "] .Length> 0) {try {_maxFileSize = long.Parse (configSettings [" maxFileSize "]);} catch {}}.} // Create StringBuilder to maintain publishing information StringBuilder strInfo = new StringBuilder ();
// Record the contents of the AdditionalInfo collection if (additionalInfo = null!) {// Record General information strInfo.AppendFormat ( "{0} General Information {0}", Environment.NewLine);.. StrInfo.AppendFormat ( "{ 0} Additonal Info: ", Environment.Newline); Foreach (String i in additionalInfo) {strINfo.appendformat (" {0} {1}: {2} ", Environment.Newline, i, additionalinfo.get (i)) }} // append the exception text strinfo.appendformat ("{0} {0} Exception Information {0} {1}", Environment.Newline, Exception.toString ()); // delete log file if is bigger Than 1MB FileInfo logInfo = new FileInfo (m_LogName);. if (logInfo.Exists && logInfo.Length> _maxFileSize) {logInfo.Delete ();}. // Write the entry to the log file using (StreamWriter sw = new StreamWriter (m_LogName , true, encoding.utf8)) {sw.write (Strinfo.toString ());}}}}
This code has made some modifications on the basis of ExceptionPublisher, and the bug is changed, and the maximum size of the log is limited by setting MaxFileSize in the configuration file. Of course, the reader likes to change the MaxFileSize configuration method. For example, I am in Byte, but the actual application may be better in KB.
This kind of exception management Application Block is very practical, because if we don't have it, we must write an exception management in your own project, but it is not necessarily universal, and it is not necessarily so powerful.