Trace and code tracing service and trace code tracing service

Source: Internet
Author: User
Tags log4net

Trace and code tracing service and trace code tracing service

First, reference this tracking service in MVC2 2 in action.

When you called Trace. write () in Web Forms, you were interacting with the Trace-Context class. this exists on your ViewPage in ASP. net mvc, but this isn' t where you wocould want to write tracing statements. by the time you 've passed the baton over to the view, there's no logic there that you 'd need to trace. instead, you 'd like to trace the logic embedded in your controllers. you might try to leverage the TraceContext class in your controller, but these statements won't ever make their way to the list of messages in the trace log (on your page or on Trace. axd ). instead, you can use System. diagnostics. trace and set up your own TraceListeners to inspect the activity in your controllers. alternatively, you can leverage a more mature logging framework such as log4net or NLog:

You debug ASP. net mvc applications just as you wowould any. NET application. tracing, however, doesn' t offer as much for MVC. instead, you can lean on the built-in TraceListeners in. NET, or utilize a good logging library like those mentioned earlier. another aspect of error logging is health monitoring.

From http://stackoverflow.com/questions/3328678/asp-net-mvc-tracing-issues>

When you call Trace. Write () in WebForm, you are interacting with the class of the Trace context. This trace information exists in your ASP. NET ViewPage, but this is not where you want to output the trace statement. When you pass the information to the View, there is no logic you need. Instead, you want to embed the tracing logic into your Controller. You may try to use TraceContext in your Controller, but these statements are never & * ### (make their way will not be translated here) it is stored in the Trace log in the form of a message list (either on your page or in the Trace. axd ). Instead, you can use System. Diagnostics. Trace to set your own TraceLinteners to observe the activities of your Controller.Alternatively, you can use other more mature frameworks, such as log4net or NLog;

Just like debugging. NET applications, debug ASP. net mvc applications. However, the tracking service does not provide more support for MVC. You can use TraceListener built in. NET or some excellent logstores mentioned above. Exception logs are used for health monitoring.

The younger brother's level of English is indeed poor, and the university has not passed level 4. But at least we found that the best application scenario of Trace is WebForm. It is better to use other methods in ASP. net mvc. So if you are not interested in the tracking service, you can turn to the left.

Those who continue reading this article will be interested in it. The following figure shows the effect.

Add the following configuration in the configuration file of an ASP. NET WebForm project:

Access a WebForm page and you will see a pile of additional content on the page.

The above page does not exist as long as you refresh the page several times, because the number of trace records is limited, and the default value is 10, which can be found on the web. config/system. set in requestLimit of web/trace. In fact, I have omitted the control tree content in the figure above, but there are still a lot of Tracked content. At least I found that even a simple TextBox contains such child controls. The following lists the tracked content.

Request details: Contains the session ID, request time, request encoding, request type, status code, and response encoding;

Tracking Information: Displays page-level event streams. If a custom trace message is created, the message is displayed in the "trace Information" section.

Control tree: Displays information about ASP. NET Server controls created on the page.

Session Status: Displays the values stored in the session Status (if any.

Application Status: Displays information about values stored in the application status (if any. This is ApplicationState.

Cookie set: Displays information about the Cookie passed between the browser and the server for each request and response. This section displays both persistent cookies and session cookies.

Header set: Displays information about the header name/value pair (information about the message body or requested resource) of the request and Response Message. Is the Header information Header.

Form set: Display name/value pairs, which are the form element values (control values) submitted in the request during the POST operation (sending back ). Is the value of the form Element submitted to the server.

Querystring set: Displays the set of server-related environment variables and request header information.

Server Variables: Displays the set of server-related environment variables and request header information.

The properties of the <trace> Configuration Section are as follows:

For the role of each attribute, refer to the documentation under MSDN.

Https://msdn.microsoft.com/zh-cn/library/6915t83k.aspx

In fact, the Trace information can be viewed through Trace. axd (Trace viewer. If the application URL is http: // localhost/SampleApplication, locate http: // localhost/SampleApplication/trace. axd to view the tracing information of the application. That is to say, the current test address is http: // localhost: 8081. If you want to access the tracking viewer, use this URL: http: // localhost: 8081/trace. axd.

Each specific request can be entered here, And the content is similar to the previous trace information. Only the actual content of the page is missing. The labels and TextBox.

The trace information can be output on the page because a class called WebPageTraceListener is called, which is a subclass that inherits TraceListener. Other subclasses such

Any inheritance of this class can output tracking information through the implementation of different sub-classes. For example, TextWriteRaceLintener can output the trace content to a txt file.

You can use this small example

Add code to Global. asax

Add two listeners to the tracking listener set.

Then, Call Trace in the page code to output two traces.

If the Flush method is not called, the trace information is not output to the text.

In ASP. net mvc, the Trace class is also called to record some Trace information. What is the result?

Simply add code to the Action method and then access the corresponding URL

The result shows that there is no hair on the page. When you enter the tracking viewer, you can find a Trace. When you enter the tracing viewer, we find that WebForm has fewer tracking information than control tree and page-level event stream. Trace. the Write content should be in the display trace information column. But this does not appear. It also proves that the configuration of the tracking service is different between WebForm and MVC. To display the tracing information on the page, you must manually add a Listener. In this way, you can only view the Trace Output content in the trace viewer. You cannot see the Trace information on the original page, that is, the pageOutput in the trace configuration section is invalid.

It is true that this tracking service will be eliminated now, such as through Trace. in this way, the output tracing information can be replaced by writing logs. You can view request headers, cookies, and other request-related information through browser Developer Tools. However, if you are too reluctant to press F12 to open the developer tool and remotely go to the server to open the log file, you can still enable this tracking service.

Finally, I would like to thank china autumn for checking a few translations. Thank you very much.

References

Https://msdn.microsoft.com/zh-cn/library/bb386420.aspx

Https://blogs.msdn.microsoft.com/webdev/2013/07/16/tracing-in-asp-net-mvc-razor-views/

Http://stackoverflow.com/questions/3328678/asp-net-mvc-tracing-issues

Related Article

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.