"Http://www.cnblogs.com/chenkai/archive/2013/01/26/2877855.html"
Often when a user request is processed on the server side. Especially for Web applications. When an exception occurs, a restore exception can be logged according to the log action. The problem occurs when you record the exception stack information. In order to track and record the server behavior. In particular, to build a simple, The unified exception handling mode is particularly important.
If there is a basic schema used to record logs and events on the server side. Then for debugging and solving the problem, it becomes simpler and more straightforward. For logging. The first manifestation is that there is a bug in the bottom of the application or in the runtime, or crash out in a particular case. More obvious behavior. The purpose of logging is to keep track of the underlying behavior of the application. Understand the procedures that are performed inside the application when an exception occurs. Helps developers and software test find the cause of the application crash. Solve problems quickly.
And there are some situations. It is difficult to develop the stage to expose the problem. Similar performance issues. If you have logging service-side behavior, you can easily identify performance bottlenecks for your application by providing a detailed execution time record. Locate these more "hidden" problems.
So it took some time to study the logging component. Talk about your personal opinion.
In the open source log management of course have to mention is Logstash.
It is a platform for application logging, event transfer, processing, management, and search. Based on HTML5, Jquery, CSS3, and SVG technologies. So it's also a cross-platform, Cross-browser. You can apply any of the ASP. Use it to unify the collection management of the Application log. The most rare and valuable provider of Web interface user queries and statistics. So the feature is that cross-platform is also open enough. [This is the power of the open source community.] Recommended.
The other one is the. NET version log4net that was ported from Java
This may have been very familiar to a bit. Log4net is a very good logging framework from the Java platform log4j ported to. Net. It is also part of the Apache fund-financed project. Log4net can help us to output log information to a variety of different targets (text files, databases, console, etc.). NET class library, it can be easily loaded into the development project, the implementation of the program debugging and operation of the log information output, provides more than. NET provided by the Debug class and the Trace class features more, the use is very simple.
I'm talking about the elmah of this article.
Elmah is actually [the error Logging Modules and handlers] abbreviation. It is a fully hot-swappable error Logging tool dedicated to ASP. The feature is that you can configure Web. config (or machine.config) to implement error logging for the entire application or even for all ASP. NET applications in IIS without having to recompile the ASP. It supports a variety of log storage methods (various databases, XML, memory storage), in addition to providing an interface for querying the log details, but also by e-mail, RSS subscription or Twitter Publishing way to notify the relevant people.
Try it separately.
The biggest feature of Logstash is beyond the cross-platform itself. Its most powerful place is its rich plug-in. Various flexible custom rules. Outputs a variety of log results. You can customize the filter for different data sources on different servers, and then output them to different plug-ins. This is a collection log for distributed Provides a good solution. Similar to monitoring logs on a B server. You can then accept the logging data on the C server and analyze it to send to D server for alarm and fault-tolerant processing. Free and open. You can collect log data at any end.
Log4net contains four major components, Logge, Repository, Appender, and Layout. Powerful. You can customize the log output level. But I think for small-scale applications. In configuration and flexibility is not enough clean.
This article tries to use Elmah in ASP. NET MVC 4.
First build the empty ASP. NET MVC 4 Project:
To add a Elmah reference:
If the adoption of VS2012 is already configured successfully on the Elmah build. In fact, this process has done two things:
A: Copy the Elmah.dll to the Bin folder of the program's root directory. and a reference to the current project.
B: Add the following to the Web. config file under the project root directory
In the Webconfig file, add the following:
1: <configsections>
2: <name= "Elmah">
3: <namerequirepermissiontype/>
4: <namerequirepermissiontype/>
5: <namerequirepermissiontype/>
6: <namerequirepermissiontype/>
7: </sectiongroup>
8: </configsections>
9:
Ten: <httphandlers>
One: <verbpathtype/>
: </httphandlers>
13:
: <httpmodules>
: <nametype/>
: </httpmodules>
About the Elmah project reference configuration is basically complete. If you're not used to NuGet, you can also add project references in a manual way.
At this point throw a nullexception in the HomeController:
1: class Homecontroller:controller
2: {
3: Public actionresult Index ()
4: {
5: //throw new NULL exception for Test
6: new ArgumentNullException ();
7: return View ();
8: }
9: }
The results are as follows:
It's obvious. When we throw an exception, there will be yellow pages with an error. Check to see if Elmah records the ArgumentNullException of this execution.
You can see that Elmah has caught the exception of the current application as scheduled. Elmah in the background to record the error message, and provide us with the query error log information interface, only need to simple operation, the basic requirements have been completed.
In fact, the principle of elmah processing. When we request the page to error. When the Yellow pages errors are returned, they are first intercepted by the httpmodules named Errorlog module. The module saves the information for this request error.-The default is to be placed in memory. Facilitates instant debugging. But when the user enters Elmah.axd to view the log information. First, httphandlers captures the request. It is then handed to the handler that specializes in handling Elmah.axd. The module returns the error log view to the user. The Elmah core technology is also based on httpmodules and httphandlers.
After adding the Elmah reference visible in Web. config, add the Elmah node as follows:
1: <elmah>
2: <!--
3:
4: More information on remote access and securing ELMAH.
5: -
6: <allowremoteaccess/>
7: </elmah>
Elmah exposes feature configuration items through this node. This configuration option is rich in functionality. The opening mentions Elmah support for storage includes:
- Microsoft SQL Server
- Oracle (Oracleerrorlog)
- SQLite (version 3) database file
- VISTADB (Vistadberrorlog); In version 1.2, it is no longer recommended
- Microsoft Access (Accesserrorlog)
- SQL Server Compact Edition (1.2 support)
- MySQL (1.2 support)
- PostgreSQL (1.2 support)
First, if you are simply referencing Elmah and not configuring what form of storage to use. Elmah is the default setting for memory storage. Although this approach is easy to develop and debug. It is recommended to persist the data for storage after the production environment is deployed. xml files or with supported databases.
Changing to a file storage method requires just adding the following code to the configuration item:
1: <elmah>
2: <!--
3: See http:
4: More information on remote access and securing ELMAH.
5:-
6: <security allowremoteaccess="false"/>
7: <errorlog type="Elmah.xmlfileerrorlog, Elmah" logpath=" ~/static/log/"/>
8: </elmah>
This configuration must confirm that the LogPath path directory is intact. The test finds an XML file that appears in a local file:
This approach is simple and intuitive. But the Elmah is designed in every run if the application goes wrong once it is caught. Create a standalone file independently. This approach can result in a large number of redundant files on the server side. And the management of these files is also a problem.
Database is still the best choice for data visualization and management. The SQlServer2008 version test is used here. The following three actions are required to support SQL Server data support in building Elmah:
A: Need to tell elmah what type of database to connect to
B:elmah How to connect to these databases. That is, the connection string needs to provide
C: The corresponding storage log log table and view stored procedures already exist in the specified database.
The actual operation first requires the Elmah node to add the following:
1: <typeconnectionstringname/>
The following SQL statements are executed in the data. Please refer to the official connection.
Elmah SQL Server Script file:http://code.google.com/p/elmah/source/browse/src/elmah/sqlserver.sql
When the Execute SQL statement finishes, the table is seen in the current database:
When you run the application again. Query the database when throw ArgumentNullException:
Visible simple and practical. In addition, all the official standards are given for SQL statement execution. There are some differences between SQL script scripts in different databases.
Use the Elmah process to send some features. Here's what you need to explain.
Elmah is an exception that is recorded and displayed by the program through HTTP Modules and HTTP handler. But if you add exception handling modules to your application. Try-catch Elmah cannot be recorded. Or throw out after a Catch. On the entire application exception chain. Only the final exception is thrown to the ASP. NET runtime Elmah component to be captured and logged.
There are a lot of people who think that adding Elmah components can handle application exceptions. Essentially Elmah is essentially a logging tool. There is no ability to handle exceptions. So if an exception occurs. The original application is not changed to the user experience. A yellow page will still appear.
The official note expressly mentions an exception.:
The Elmah catch exception is an error event based on the HttpApplication object.
If some processing in a software project causes the HttpApplication event to fail to be triggered (for example, after an exception has not yet been performed Application_Error, the Server.ClearError () method is executed,
Prevents the triggering of an error event, for example, if an exception is caught by Try-catch and does not throw again, then the exception will not eventually trigger the error event)
There are many logging tools, such as the famous log4net and the logging application Block in the Enterprise Library. It can be said that these tools, relatively elmah, are too heavyweight, they can record various log information, such as monitoring the changes in the code variables, periodic records to the file for other applications for statistical analysis work, tracking code run-time trajectory as a basis for future audits , acting as a debugger in an integrated development environment, to print debugging information for the code to a file or console. Therefore, if you are simply logging the error log for ASP. And Elmah is open source and clean enough. preferred.
Elmah Log Logging Component