How To log internal BotDetect ASP.NET 1.1 CAPTCHA errors (BotDetect v2.0)

Please Note

The information on this page is out of date and applies to a deprecated version of BotDetect™ CAPTCHA (v2.0).

An up-to-date equivalent page for the latest BotDetect Captcha release (v3) is BotDetect v3 ASP.NET Captcha How To guides.

General information about the major improvements in the current BotDetect release can be found at the What's New in BotDetect v3.0 page.

Since version 2.0.3, BotDetect ASP.NET CAPTCHA ships with a centralized, non-intrusive, BotDetect-only, highly customizable error logging utility based on the log4net project. This page describes how to use this utility in your projects.

Table of Contents

Log BotDetect CAPTCHA errors to a text file

Please Note

Since this guide shows how to log errors to a text file in the server filesystem, it will only work in full trust environments. If you need error logging in environments where your application is not allowed to write to the filesystem, you can investigate other log4net output methods (for example, logging to a database).

Step-by-step Instructions

  • Find the Troubleshooting sub-folder in your BotDetect ASP.NET CAPTCHA installation folder (C:\Program Files\Lanapsoft\BotDetect 2.0 CAPTCHA\ASP.NET 1.1\Assembly\Troubleshooting by default)
  • Copy the Lanap.BotDetect.Troubleshooting.dll and log4net.dll files to your application's Bin folder
  • Create a text file called error.txt in the same folder your application's web.config file is in, and make sure the appropriate IIS user account has Modify permissions for it.

    Which user account is used depends on the IIS version you are running and your IIS configuration. By default, you will use:

    • IIS 5.0 or 5.1: ASPNET
    • IIS 6.0 or 7.0: NETWORK SERVICE
    • IIS 7.5: the ApplicationPoolIdentity user account for your application's AppPool

    You can set the required permissions from the command line by running:

    cacls error.txt /G "ASPNET":C /E /C
    cacls error.txt /G "NETWORK SERVICE":C /E /C
    cacls error.txt /G "IIS AppPool\TODO:AppicationPoolName":C /E /C
  • Add the following lines at the top of the <configuration> section in your application's web.config file:
    <configSections>
      <section name="log4net" 
        type="log4net.Config.Log4NetConfigurationSectionHandler, 
        log4net" />
    </configSections>
    
    <log4net>
    
      <!-- Errors are logged to a 'error.txt' file  -->
      <appender name="ErrorFileAppender" 
          type="log4net.Appender.FileAppender">
        <file value="error.txt" />
        <appendToFile value="true" />
        <lockingModel 
          type="log4net.Appender.FileAppender+MinimalLock" />
        <layout type="log4net.Layout.PatternLayout,log4net">
          <conversionPattern 
            value="%date [%thread] %type - %n%n%message%n%n" />
        </layout>
      </appender>
    
      <!-- Error logging is enabled, comment-out to disable -->
      <logger name="ErrorLogger">
        <level value="ERROR" />
        <appender-ref ref="ErrorFileAppender" />
      </logger>
    		
    </log4net>
    

    This will register a custom log4net configuration section with all the necessary log4net settings.

  • Add the following lines at the top of the <system.web> section, just above the <httpHandlers> element:
    <httpModules> 
      <add type="
        Lanap.BotDetect.Troubleshooting.LoggingModule, 
          Lanap.BotDetect.Troubleshooting" 
        name="LoggingModule" />
    </httpModules>

    This will register the error tracking HttpModule.

Explanation

The Lanap.BotDetect.Troubleshooting.dll assembly contains a special HttpModule (called LoggingModule), which handles the Application_OnError event, for errors originating in BotDetect internal code only. All other errors are ignored, and you can handle them in any way that suits your application needs.

Sample Project

The approach described in the previous section is implemented in the troubleshooting sample shipped with BotDetect, so you can check it's (C# or VB.NET) source code for further details and web.config file examples.

Using the Troubleshooting module on Production servers

  • The troubleshooting module shipped with BotDetect only handles errors occurring in BotDetect internal code, and should not (in principle) significantly impact your application performance.
  • However, this may vary depending on your server load and configuration settings; if you find that it's slowing your application down, you can simply turn it off by deleting the aforementioned <httpModules> element from your web.config file.

Diagnosing and reporting the problem

  • The error.txt file (in the same folder as your web.config file) contains details about all BotDetect internal errors, and if you send it to us, we can help you debug the problem and determine the exact error cause.
  • If this file is empty and you are still experiencing difficulties, try to reproduce the problem using one of the samples shipped with BotDetect installations instead of your application code. Try changing the sample code to be more like the code you are using – does the problem start occurring after a particular change?
  • If the problem occurs only in your application and never when using the samples, there is probably an error in your code that is using BotDetect CAPTCHA, and you should review it carefully.

Advanced logging configuration and log4net license

Since the LoggingModule uses the open source log4net logger (http://logging.apache.org/log4net/), you can customize the exact method of error logging to best suit your specific needs: you can write logs to a text file, or a database, the console etc.

There is a plethora of different options and settings you can use, and we encourage you to learn more about them in the log4net manual at http://logging.apache.org/log4net/release/manual/introduction.html.

The log4net license can be can be found at http://logging.apache.org/log4net/license.html, as well as in the Assembly\Troubleshooting sub-folder of your BotDetect ASP.NET CAPTCHA installation folder (log4net license.txt and log4net notice.txt files, as per the terms of use).

BotDetect CAPTCHA error log format

Each error log contains the following information:

  • The date and time when the error occurred
  • The detailed stack trace of the error and all inner exceptions
  • The Session state contents relevant to BotDetect CAPTCHA functionality:

    • the Session ID
    • values of all Session State keys generated by BotDetect
    • the collection of codes active for the current Session
    • time of generation, contents and usage tracking of each active code
  • The client browser raw agent string

BotDetect CAPTCHA error log example

2007-02-06 16:55:45,125 [4244] 
  Lanap.BotDetect.Troubleshooting.ErrorLogger - 

Lanap.BotDetect.CaptchaGenerationException: 
  An error occurred in the BotDetect control internal code. - 
  Captcha binary data generation failed. - Check inner 
  exception for details. ---> 
  System.NullReferenceException: 
  Object reference not set to an instance of an object.
	
  at System.Drawing.Drawing2D.GraphicsPath.AddString(String s, 
    FontFamily family, Int32 style, Single emSize, 
    Point origin, StringFormat format)

  at Lanap.BotDetect.CurvedText.TextEffect(Graphics g, 
    String strText, Font font, Brush brush, Pen pen) 
  in C:\Projects\BotDetect\Component\DotNet 1.1\SourceCode\
      Component\BotDetectControl\CaptchaCore\CaptchaImage\
      Support\CurvedText.cs:line 174
  
  at Lanap.BotDetect.CurvedText.DrawText(RectangleF rect, 
    Graphics g, Font font, String strText, Brush brush, 
    Pen pen) 
  in C:\Projects\BotDetect\Component\DotNet 1.1\SourceCode\
    Component\BotDetectControl\CaptchaCore\CaptchaImage\
    Support\CurvedText.cs:line 28

  at Lanap.BotDetect.ChalkboardTextGenerator.DrawText(
    String text, Bitmap textDestination, Color 
    backgroundColor) 
  in C:\Projects\BotDetect\Component\DotNet 1.1\SourceCode\
    Component\BotDetectControl\CaptchaCore\CaptchaImage\
    TextGenerator\Generators\ChalkboardTextGenerator.cs:
    line 45

  at Lanap.BotDetect.ImageGenerator.GenerateImage(
    String textToRender, TextStyleEnum textStyle, 
    Color backgroundColor) 
  in C:\Projects\BotDetect\Component\
    DotNet 1.1\SourceCode\Component\BotDetectControl\
    CaptchaCore\CaptchaImage\ImageGenerator.cs:line 31
  
  at Lanap.BotDetect.ImageGenerator..ctor(String textToRender, 
    TextStyleEnum textStyle, Size size, Color backgroundColor) 
  in C:\Projects\BotDetect\Component\DotNet 1.1\SourceCode\
    Component\BotDetectControl\CaptchaCore\CaptchaImage\
    ImageGenerator.cs:line 25
   
  at Lanap.BotDetect.CaptchaCore.GenerateImage(String code, 
    TextStyleEnum textStyle, Size imageSize, 
      ImageFormatEnum imageFormat, Color backgroundColor) 
  in C:\Projects\BotDetect\Component\DotNet 1.1\SourceCode\
    Component\BotDetectControl\CaptchaCore\CaptchaCore.cs:
    line 185

  at Lanap.BotDetect.CaptchaHandler.DrawImage(HttpContext 
    context) 
  in c:\projects\botdetect\component\dotnet 1.1\sourcecode\
    component\botdetectcontrol\httphandler\captchahandler.cs
    :line 83
  
  --- End of inner exception stack trace ---
   
  at Lanap.BotDetect.CaptchaHandler.DrawImage(HttpContext 
    context) 
  in c:\projects\botdetect\component\dotnet 1.1\sourcecode\
    component\botdetectcontrol\httphandler\captchahandler.cs:
    line 101
  
  at Lanap.BotDetect.CaptchaHandler.ProcessRequest(HttpContext 
    context) 
  in c:\projects\botdetect\component\dotnet 1.1\sourcecode\
    component\botdetectcontrol\httphandler\captchahandler.cs:
    line 30
  
  at System.Web.CallHandlerExecutionStep.System.Web.
    HttpApplication+IExecutionStep.Execute()
   
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep 
    step, Boolean& completedSynchronously), 

SESSION

ID : z4zeiu45wxykdm45pfj1ha45

LBD_CaptchaCodeCollection__csharpbotdetecttroubleshootingdemo_
  default_samplecaptcha : 

CODE COLLECTION

37774465 : CAPTCHA CODE

code : B4QKK
generated at : 6.2.2007 16:55:45
image requested: True
sound requested: False
, 

BROWSER

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 
  Avant Browser; Avant Browser; InfoPath.1; .NET CLR 1.1.4322;
  .NET CLR 2.0.50727)

Please Note

The information on this page is out of date and applies to a deprecated version of BotDetect™ CAPTCHA (v2.0).

An up-to-date equivalent page for the latest BotDetect Captcha release (v3) is BotDetect v3 ASP.NET Captcha How To guides.

General information about the major improvements in the current BotDetect release can be found at the What's New in BotDetect v3.0 page.

language: English Español Tiếng Việt