Chapter 4: Troubleshoot and debug web applications

Prevent and troubleshoot runtime issues

  • Troubleshooting performance, security and errors
  • using performance wizard (Vs2012)
  • Using VS profiler (Analyzer | Profiler)
  • Using Performance Monitor
  • NLog/log4net for logging
  • Tracing, Trace.WriteLine("Message")/Write/WriteIf/WriteLineIf
<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="myListener" type="System.Diagnostics.TextWriterTraceListerner" initializeData="TracingInfo.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>
  • Error logging
    • HandleErrorAttribute
    • overriding controller's OnException: protected override void OnException(ExceptionContext pContext)
  • Enforcing conditions by using code contracts
internal Article GetArticle(int pId)
{
  System.Diagnostics.Contracts.Contract.Requires(id > 0);
  System.Diagnostics.Contracts.Contract.Ensures( Contract.Results<Article>() != null );
  /// some work here
}

[ContractInvariantMethod]
protected void ManageInvariant()
{
  System.Diagnostics.Contract.Invariant(this.Id < 0 );
}
* Preconditions
* Invariants
* Postconditions

install Code Contracts Editor Extensions from VS Gallery

  • Enabling and configuring health monitoring
    • bufferModes
    • providers
    • profiles
    • rules
    • eventMappings

Design an exception handling strategy

  • Handling exceptions across multiple layers
  • use Application_Error in Global.asax to handle error pages
  • set error information in Web.config as below:
  <customErrors mode="RemoteOnly" default_redirect="ErrorManager/ServerError">
    <error statusCode="400" redirect="ErrorManager/Status400" />
    <error statusCode="403" redirect="ErrorManager/Status403" />
    <error statusCode="404" redirect="ErrorManager/Status404" />
  </customErrors>

HTTP 500 erros are generally handled by filters or OnException handlers.
set in <system.webServer> of Web.config

  • Handle first exception
  AppDomain.CurrentDomain.FirstChanceException += OnFirstChanceException;

  protected void OnFirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
  {
  }

Test web application

  • running unit tests
    • creating mocks by Fakes Assembly (shim/stub)
  using(ShimsContext.Create())
  {
    System.Fakes.ShimDateTime.NowGet = () => new DateTime(2010,1,1);
    TestMethodNow();
  }

Debug a Windows Azure application

  • use IntelliTrace
  • use Remote Desktop
--------------------------- 知道的更多,不知道的也更多 ---------------------------
原文地址:https://www.cnblogs.com/mryux/p/4792941.html