Put your message here! Contact me for more information
 
 







 

Archive for the ‘Random Walk’ Category


 

Here is a quick guide to configure log4net for general asp.net 2.0 and asp.net 3.5 web projects. While you can find general guides for setting up log4net, more advanced configuration will require further digging through Log4Net documentations. My intention is to provide a quick, 5 minutes overview of configuring log4net for a web project with a more complicated, nested logging configuration.

My goal is to have 2 independent loggers working in parallel. One logs application errors (such as exceptions or serious errors that need to be reviewed later) to the Event log of Windows, and the other will log all general messages to a text file. The reason why 2 loggers are needed is because the Windows server is configured to send out email notifications on “Error” events to notify whoever in charge of maintaining the application. The general logging file containing all logging messages will provide more application-level debugging info.

Installing and Setting up Log4Net
Log4Net is a drop-in DLL file that you can download from the Apache project site. The binary is compatible with ASP.NET 3.5 and ASP.NET 2.0, as well as the Mono platform. After adding the reference to the DLL to your project, create the Global.asax file and add one line to the Application_Start method to initialize log4net at application start up.

void Application_Start(object sender, EventArgs e)
{
  log4net.Config.XmlConfigurator.Configure();
}

In the web.config file, add the log4net section handler to the configSections block



  
    

Basically we have 2 “appender” loggers and their above configurations are quite self-explanatory. The first appender, EventLogAppender, has a level range filter, which will filter out only FATAL or ERROR log entry. Then it has 2 mappings to map the different log levels to the correct Window’s Event message type. Finally, the layout section defines how the log text should look like.

For the 2nd logger, we use a RollingFile appender, which will automatically rotate the files based on the log file size. We define where to log the files to, what the maximum size is, and how many files we would like to keep. We don’t need to configure a filter for this logger since we are collecting all entries for debugging purposes, but you can add a range filter or a pattern filter without any problem.

Finally, we define the root logger. This root-level logger is the one logger receiving our log message and it will distribute these messages to all of the configured referenced appenders.

To use log4net in the program, all you need to do is add “using log4net;” to the top of the file, and initilize a static logger as follow:

public class PageBase : System.Web.UI.Page
{
  public static readonly ILog log = LogManager.GetLogger("Logger");
  public PageBase()
  {
     log.Debug("This is a debug message");
     log.Info("This is an info message");
     log.Error("This is an error message");
  }
}

Notes

  • Log4net is meant to be un-obtrusive and it won’t throw any error whatsoever, even in the case of invalid configuration. This means that you won’t know why it fails if log4net happens to fail. As the manual says, log4net is not meant to be a reliable logging mechanism. So watch out there
  • For the file appender, if you log into a sub-folder within the web project, you should take extra precaution steps to protect the one folder from public eyes. For example, denying all anonymous users from accessing the “log” folder by adding the following section into the web.config

  
    
      
        
      
    
  

References

view comments
 

I randomly ran into Steven Levithan’s blog while searching to get an idea of how JavaScript handles Unicode. Steven is a JavaScript - Unicode and Regular Expression expert. He has a cool section called “Code Challenge” with some good food-for-thoughts challenges. It’s really JavaScript being pushed to the max, in terms of brevity, creativity, and obscurity. Check out Stephen’s “Roman Numeral Convert” challenge for example.

Reading through the comments, I picked out a nugget explaining a JavaScript behavior which actually caused me some unexpected issues with TubeCaption’s Captionizer. Steven explained best in his original comment

… you might have already realized this, but the unary + operator and parseInt are not equivalent. + can convert strings to numbers, and returns NaN if the element cannot be converted. parseInt (which takes an optional second argument for the radix) does the same thing, but also extracts leading numbers from strings. E.g., parseInt(”12x”) returns 12, while +”12x” returns NaN. Additionally, parseInt and + make different assumptions about the radix when there’s a leading zero. +”012″ returns 12, but parseInt(”012″) returns 10. The leading zero causes parseInt to treat it as an octal number in probably all browsers, despite octals being summarily deprecated in ES3. Of course, you can use parseInt(”012″,10) to get around that.

Here is a quick demo of how parseInt() behaves.

For the SRT import feature of TubeCaption’s Captionizer, I heavily relied on parseInt() to get the different time values. I was caught by surprise when a user notified me that his SRT file could not be imported into the timeline. After some debugging, it turned out that some values had padding values and the parseInt() returned incorrect results in octal instead of decimal. I wish I had known about the “+” trick and the subtlety of JavaScript at the time.

view comments
 

It’s a mouthful for the title, but it is true.  I just installed IE8 Beta 2 to try out the new features and see if IE’s javascipt engine gets any speed bump.  So far IE8 is running only as fast as IE7 in my tests, while Firefox 3 runs screamingly fast (read 500% faster, seriously).

I’m impressed with IE8b2’s new Developer tool, a Firebug-like tool.  Surprisingly, the JavaScript debugger actually WORKS!  And it feels much smoother to debug JavaScript than Firebug (Firebug still suffers from some reading/refreshing glitches and doesn’t feel as responsive).

ie8b2 debugging

I was able to step into an object created using prototype’s Class.create() with no problem using F10 step over, F11 step-in keys work as expected.  Nice!  There is also a Profiler which would be totally awesome and for sure I’ll be using it extensively to optimize the JavaScript for my upcoming application.   Finally there’s some light at the end of the tunnel, which Microsoft actually give us developers what we’ve been dreaming for:  making IE sucks less.

I also managed to crash IE8b2.

I was actually running the debugger and stepping in my code.  Then I noticed and clicked clicked on “Browser Mode IE7″.  The entire application froze up.  My explanation is that the main browser process or thread is attached to the debugger, which is running in line-debugging.  Switching “Browser Mode” to a different setting requires the main browser to switch to a different view.  However, since the main process is stuck with the debugger, and the debugger is waiting for the main process to switch browsing mode, we have a deadlock situation.

I had to end-task the sucker.

And suddenly, my Firefox 3.1 started to constantly crash, even in safe-mode (running Firefox.exe using -safe-mode flag).  I chose to permanently disable all add-ons and Firefox managed to get to my favorite homepage, the about:blank page.   However, whenever I went to Yahoo Mail, the browser just crashed without even a trace in the Windows Event Application log.  All I got is the Mozilla Crash Reporter to play with.   With all my add-ons disabled, I was determined to find out what was killing Firefox, in safemode.  Apparently, something was almost as determined as I to come get Firefox, even when it’s waving the “-safe-mode” flag.

First, I went to Firefox Options, trying to find out what happened.  Clicking on the “Applications” tab would automatically crashed Firefox, weird. I went into Options, then Managed Add-ons.  I disabled all plugins, then restarted Firefox and try Yahoo Mail.  It worked.

Since IE8 was the only application I installed before Firefox started to crash, I proceed to enable all plugins but Microsoft’s.  Yahoo Mail worked still.  I then enabled the Windows Presentation Foundation (WPF) plugin and, bam, Firefox crashed again.  The culprit is the new WPF plugin that IE8 beta 2 secretly installed into Firefox.  It’s very nice of you, IE8 installer.  Should I call you mal-ware now since you just make one of my main application mal-functioned?  You just make me almost lose anything trust (left) that I have for installing Microsoft’s stuff.  Good thing trying out the new IE8 (which proves IE7 is that horrible for developer still!) is compelling enough so I let you go easy this time.

So IE8 Beta 2 testers, disable Windows Presentation Foundation plug-in for Firefox will help you stop IE8 from sabotaging your Firefox browsing experience with blazingly fast javascript execution, which IE can only cry foul by crashing itself.

view comments
 

add your captions on TubeCaption.com
view comments
 

Recently I had to configure my VPS at Slicehost.com to send out emails for my TubeCaption application. Due to the time constraint of the launch, we decided to outsource the mail delivery to Gmail SMTP server.

Using Gmail SMTP server with ActionMailer

When I first deployed the application, I didn’t configure sendmail correctly (I’m a total stranger to sendmail or postfix) and went ahead with Google’s SMPT server.  Since I am using Google Apps to host the mailboxes, I created an extra account specifically to use for mail delivery. In my development.rb and production.rb file, I have ActionMailer configured as follow:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:tls => true,
:address => “smtp.gmail.com”,
:port => 587,
:domain => “tubecaption.com”,
:authentication => :plain,
:user_name => “sample-mailer-account@tubecaption.com”,
:password => “the-very-secured-password”
}

Within my ActionMailer handler, I have a function to setup the default params as follow

class Postoffice < ActionMailer::Base
def setup_default_params
@from = “my-email@tubecaption.com”
@headers = “Reply-to my-email@tubecaption.com”
@subject = “TubeCaption.com”
@sent_on = Time.now
@content_type = “text/html”
end
end

Then for each email, before sending, I invoke setup_default_params() to have all the params setup. The site could now sending out emails, albeit slow. ActionMailer had to open a connection to GMail, authenticated, and then started writing the content as a stream of text. The whole process took from 1 to 3, or even 4 seconds some time on my machines, which means the mongrel process was also stuck there waiting.  But the mailing piece worked. However, I came to know the real issue:  all the emails were marked as SPAM or JUNK if they are sent to GMail or Yahoo.

I talked to a friend of mine, who has more knowledge in the emailing world. He suggested the cause of this is because emails sent from my VPS failed the reversed DNS lookup verification. Yahoo mail servers perform a reverse DNS look up to check my domain’s DNS records, while the SMTP server I used was from Gmail. Hence the emails were marked as spam in Yahoo and junk in Gmail due to the DNS check mis-match.

So I decided that it was about time for me to configure sendmail for the box and switch ActionMailer to use sendmail instead.

ActionMailer with local sendmail

Truthfully speaking, I would love to get away as far as I can from configuring sendmail. The 4th edition O’Reilly book on Sendmail is more than 1,000 pages! I only want something that can send out emails… Luckily, there is yum.

# sudo yum install sendmail

Now I switch my ActionMailer config back to use sendmail. Since my sendmail is installed in the default folder, I don’t need to configure ActionMailer any further. In production.rb, I have

ActionMailer::Base.delivery_method = :sendmail

ActionMailer now delivered mails using the local sendmail program.

Using the console, I sent out a test email to my yahoo acount.   Everything works.  Then, I received a notice from my server, telling me I have new mails at /var/spool/mail. Great! I checked the mail log, and saw …

—– The following addresses had permanent fatal errors —–
<nworld3d@yahoo.com>
(reason: 553 Mail from XX.XXX.XXX.XXX not allowed - 5.7.1 [BL23] Connections not accepted from IP addresses on Spamhaus XBL; see http://postmaster.yahoo.com/550-bl23.html [550])

—– Transcript of session follows —–
… while talking to b.mx.mail.yahoo.com.:
<<< 553 Mail from XX.XXX.XXX.XXX not allowed - 5.7.1 [BL23] Connections not accepted from IP addresses on Spamhaus XBL; see http://postmaster.yahoo.com/550-bl23.html [550]
… while talking to d.mx.mail.yahoo.com.:
<<< 553 Mail from XX.XXX.XXX.XXXnot allowed - 5.7.1 [BL23] Connections not accepted from IP addresses on Spamhaus XBL; see http://postmaster.yahoo.com/550-bl23.html [550]
…….
<<< 553 Mail from XX.XXX.XXX.XXX not allowed - 5.7.1 [BL23] Connections not accepted from IP addresses on Spamhaus XBL; see http://postmaster.yahoo.com/550-bl23.html [550]
554 5.0.0 Service unavailable

…….

Final-Recipient: RFC822; nworld3d@yahoo.com
Action: failed
Status: 5.5.0
Diagnostic-Code: SMTP; 553 Mail from XX.XXX.XXX.XXX not allowed - 5.7.1 [BL23] Connections not accepted from IP addresses on Spamhaus XBL; see http://postmaster.yahoo.com/550-bl23.html [550]
Last-Attempt-Date: Tue, 3 Jun 2008 14:02:54 -0400

OUCH! My VPS got black-listed! Somehow the Spamhaus XBL list decided to black list the IP address of the server. Probably because I had port 25 opened in the firewall and somebody has taken advantage of my lack-of-linux-sysadmin-skills to start relaying spams.  CRAP! Yahoo outrightly refused any SMTP connection from my server because it was marked as a spammer-wannabe .  Good thing Yahoo included the  link to their help page and references to Spamhaus XBL.

I went to Spamhaus website and looked up my server’s IP:

The CBL (composite blocking list) was blocking my server.  I went ahead and request a removal from the list.  Afterwards, the CBL list showed

A few minutes later, I checked Spamhaus and it’s not showing the IP listed anymore.  Phew!

It would take a while before this list is propagated through out the internet.  At the moment, I still can’t connect to Yahoo’s SMTP server and my mails is still delivered right into Google’s Junk mailbox.  Hopefully a few hours more and my server would be good to go.

I don’t know if Spamhaus is my friend or foe.  I have a hard time deciding it.  I don’t know what people’s experiences with emailing services in general, but I can tell it’s a pain in the butt, and everything will only get worse from here.  I wonder what it will be like when ipv6 is used…

view comments