How To log internal BotDetect ASP.NET 1.1 CAPTCHA errors
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.
- Log BotDetect CAPTCHA errors to a text file
- Advanced logging configuration and log4net license
- BotDetect CAPTCHA error log format
- BotDetect CAPTCHA error log example
Log BotDetect CAPTCHA errors to a text file
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 log.txt in the same folder your application's web.config file is in, and make sure the NETWORK SERVICE user account has Modify permissions for it.
- 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> <!-- Define the output appenders --> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="log.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> <!-- Setup the root category, add the appenders and set the default priority --> <logger name="ErrorLogger"> <level value="ERROR" /> <appender-ref ref="FileAppender" /> </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.ErrorTrackingModule, Lanap.BotDetect.Troubleshooting" name="ErrorTrackingModule" /> </httpModules>This will register the error tracking HttpModule.
Explanation
The Lanap.BotDetect.Troubleshooting.dll assembly contains a special HttpModule (called ErrorTrackingModule), 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 log.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 How To use BotDetect CAPTCHA, and you should review it carefully.
Advanced logging configuration and log4net license
Since the ErrorTrackingModule 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 occured in the BotDetect control internal code. -
Captcha binary data generation failed. - Check inner
expection 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)