Mvc api real-time log viewing

Source: Internet
Author: User

Sometimes some MVC APIs. When parameters are passed in. . Generally, log is written. It is troublesome. Inconvenient

This section describes how to display logs in real time.

First, you need an object signalr to download it from git.

Vs packing cannot be found.

This is actually websocket.

 

The idea is:

1 -- route entry configuration.

  Public   Static   Class  Webapiconfig {  Public   Static   Void  Register (httpconfiguration config) {config. routes. maphttproute (Name:  " Defaapi API  "  , Routetemplate:  "  API/{controller}/{ID}  "  , Defaults:  New {Id = Routeparameter. optional });  // Config. Services. Replace ( Typeof (Itracewriter ), New  Dynamictrace ());}} 

2. Create a new virtual class, which is equivalent to a hub and broadcasts logs to the client.

 
[Hubname ("Trace")]Public ClassTracehub: hub {}

3. Create a New signalrbase that allows any class to inherit.

Public Abstract ClassSignalrbase <t>WhereT: iHub {PrivateLazy <ihubcontext> hub =NewLazy <ihubcontext> () => globalhost. connectionmanager. gethubcontext <t>());ProtectedIhubcontext hub {Get{ReturnHub. Value ;}}}

4. Record the current time, access method, and so on. Implementation class.

    Public   Class Dynamictrace: signalrbase <tracehub> , Itracewriter {  Public   Void Trace (httprequestmessage request, String Category, tracelevel level, action <tracerecord> Traceaction ){  If (Level! = Tracelevel. Off) {tracerecord record = New  Tracerecord (request, category, level); traceaction (record); log (record );}} Private   Void  Log (tracerecord record ){  VaR Message = New  Stringbuilder (); message. appendmessage (record. level. tostring (). toupper (); message. appendmessage (datetime. Now. tostring ());  If (Record. Request! = Null ) Message. appendmessage (record. request. method. tostring (), notempty ). appendmessage (record. request. requesturi. tostring (), notempty); message. appendmessage (record. category, notempty ). appendmessage (record. operator, notempty ). appendmessage (record. operation ). appendmessage (record. message, notempty );  If (Record. Exception! = Null  ) Message. appendmessage (record. Exception. getbaseexception (). Message, notempty); hub. Clients. All. logmessage (message. tostring ();} func < String , Bool > Notempty = (text) =>!String  . Isnullorwhitespace (text );} 

5. Method of logging, string Addition

   Public   Static Stringbuilder appendmessage ( This Stringbuilder Sb, String Text, func < String , Bool > Predicate = Null  ){  If (Predicate! = Null ){  If  (Predicate (text) {sb. append (  "   "  ); Sb. append (text );}}  Else  {Sb. append (  "   "  ); Sb. append (text );}  Return  Sb ;} 

Use signalr to broadcast. Broadcast the Logs accessed by the API to the client in real time.

Signalr is a websocket encapsulation.

 

Then, these logs will be output on the page

Finally, create an HTML file to connect to the broadcast center we just created. Receive logs

 <!  Doctype html  >  <  Html  Xmlns  = "Http://www.w3.org/1999/xhtml"  >  <  Head  >  </  Head >  <  Body  >      <  Ul  ID  = "Messages"  > </  Ul  >      <  Script  Type  = "Text/JavaScript"  SRC  = "Scripts/jquery-1.7.1.js" > </  Script  >      <  Script  Type  = "Text/JavaScript"  SRC  = "Scripts/jquery. signalR-1.0.0-alpha2.js"  > </  Script  >      <  Script  Type  = "Text/JavaScript" SRC  = "Signalr/hubs"  > </  Script  >      <  Script  Type  = "Text/JavaScript"  >  $ (  Function  () {Hub  = $. Connection. Trace; hub. Client. logmessage  =   Function  (Data) {$ (  '  # Messages  '  ). Prepend (  '  <Li>  '  +  Data  +   '  </LI>  '  ) ;}; $. Connection. Hub. Start ();});  </  Script  >  </  Body  >  

See:

 

I requested an API on the left. Address. The newly created HTML real-time receiving broadcast is printed on the right.

He is not good at writing blogs .. Let's take a look.

 

ReferArticle

Http://www.cnblogs.com/kesalin/archive/2012/11/09/signalr_push.html (using signalr for push in ASP. net mvc)

 

 

 

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.