Friday, July 30, 2010

SMTP Connection Test Script with ASP.NET


Slightly off topic today, one of the few posts not about Sitecore CMS.
A small challenge I had to deal with – troubleshoot a number of SMTP servers, trying to figure out which one can actually relay.
As basic as this sounds, I have not found a ready to go ASP.NET script on the web that can help me. So I decided to build my own.

The idea is simple – use standard SmtpClient class, wrap the Send() method in try/catch and output the exception.

SmtpClient client = new SmtpClient(server, int.Parse(port));
client.Credentials = new System.Net.NetworkCredential(username, password);
MailMessage mailMessage = new MailMessage(from, to);
mailMessage.Subject = "Test Email";
mailMessage.Body = "Hello, this is a test email from BayNET. Please ignore.";
try
{
   // trying to send...
   client.Send(mailMessage);
   Response.Write("Success!!!");
}
// catching SmtpException 
catch (SmtpException exception)
{
   Response.Write(String.Format("Cannot send mail. Status Code {0}. Details:{1}", exception.StatusCode, exception.Message));
}

The files can be downloaded from here.

Ideas were taken pretty much from the folks at stackoverflow.com.

Happy SMTP relaying!

Thursday, July 29, 2010

Sitecore Logging Part 3. Adding custom parameters to the log.


Summer…I am on a blog posting spree :-)

This is a continuation of the sage about SQL logging for Sitecore CMS. As I previously blogged, you can easily have Sitecore log to a SQL database instead of a flat text file.
Now what if we take it one step forward and have Sitecore output more information than we had before, including Sitecore Context User, Sitecore Context Item Id and raw server URL?
Well, after some digging, here is the solution for you.

Sitecore Error in Package Designer/Installation Wizard


I just got the following exception in the Package Designer on my fairly clean Sitecore 6.1 install.

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.


Exception Details: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ReflectionTypeLoadException: Unable to load one or more of the requested types.
Retrieve the LoaderExceptions property for more information.]
System.Reflection.Module._GetTypesInternal(StackCrawlMark& stackMark) +0
System.Reflection.Assembly.GetTypes() +96
Sitecore.Shell.Applications.Installer.Commands.Commands.Init() +48
Sitecore.Shell.Applications.Installer.Commands.Commands..cctor() +6

[TypeInitializationException: The type initializer for
'Sitecore.Shell.Applications.Installer.Commands.Commands' threw an exception.]
Sitecore.Shell.Applications.Installer.Commands.Commands.Init() +36
Sitecore.Shell.Applications.Installer.Designer..ctor() +10

Based on the findings from our support portal, I found out that quite a few customers had the same issue and that is it also popping up in Installation Wizard.
So I decided to publish a quick post about it.

The stack trace indicates that the Installer app is trying to load some commands via reflection and obviously fails during this process.
By merging the bin directory of my Sitecore installation with the clean distributive, I found out that a few assemblies were somehow different, specifically Sitecore.Kernel.dll and Interop.Shell32.dll. So I copied them over from the clean distributive…and that resolved the issue!
It begs the question how it happened specifically, and my hunch is that somehow the DLLs got overwritten during the build process by Visual Studio, but let’s keep that as mystery.

Happy troubleshooting!

Wednesday, July 28, 2010

Sitecore Logging Part 2. Dealing with the Exceptions


As as follow up to the initial post about SQL based logging in Sitecore, here is another quick tip about how to include exception stack trace into your log database.

Tuesday, July 27, 2010

Sitecore Logging. Quick update.


Remember in the last post about SQL based logging I mentioned that there is an internal buffer that log4net has before it dumps all into the database.
While it seems to be quite useful when running in production, in development environment you would want to see immediate messages in the logs, especially when troubleshooting.
In order to do that, simply add the “bufferSize” section for your ADONetAppender and set the value to “1”:

<appender name="ADONetAppender_SqlServer" type="log4net.Appender.ADONetAppender" >
<bufferSize value="1" />

If the buffer is not explicitly set, it will be defaulted to 512 :-)

Happy logging!