profile for DaveHogan at Stack Overflow, Q&A for professional and enthusiast programmers
LinkedIn Twitter FaceBook GitHub

Dave Hogan

Hi, I'm Dave

I'm a .NET / C# developer based in the South of England who specialises in web applications using javascript, html and css. You can contact me if you have any questions.

Logging with nLog

Previously when I needed to log information to a log file I've always created a smaller logging class and this typically suffices, however recently I needed something a bit more complex. After some investigation I decided to give nLog a shot. The only thing it didn't seem to support out-of-the-box was retrieving configuration information from the web.config. I posted on Stack Overflow to see if it just me missing something obvious but it didn't appear to be. As a result I created a new layout renderer to achieve just this.

Step 1 - Add this class to your project.
public class AspNetConfigValueLayoutRenderer : LayoutRenderer
    public string Variable { get; set; }

    protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        if (this.Variable == null)
        HttpContext context = HttpContext.Current;
        if (context == null)
        builder.Append(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings[this.Variable], CultureInfo.InvariantCulture));
Step 2 - Add this to your nLog config file.
   <add assembly="YOURASSEMBLYNAMEHERE" /> 

Step 3 - You can now use the following to access variables within a web configuration file

GDI+ .NET Framework

If you've been over to the "my websites section" you'd have noticed a website / project called Knights Tour. Well this was my first attempt at doing GDI+ and I was simply amazed how powerful this library can be. Naturally being a web based developer, I was thinking up ways to utilize this. I could think up loads of ways of using this in a windows application, but not many using for a webapp. I was tempted at using it for a fancy dynamic button control but that didn't really seem worth while and would the performance be a problem? I remembered a simple game I played a few months ago and decided this would be a prime candidate for converting into a webapp using GDI+.
Another thing I decided to do whist working with GDI+ was to create a tool to quickly covert RGB into HEX.

Secure Sockets Connection

I've recently been working on a client / server project which required communication over a custom port. In this case it was 700. Easy I thought, I'll simply use the System.Net.Sockets namespace create a socket connection and off we go.
Well things didn't workout that easy. The port required SSL which I couldn't find much documentation about. All SSL communication information I could find was all about HttpWebRequests etc... I then looked more into the framework and came across SSLStream, which can be used in conjunction with TcpClient.

Here is my successful Connect method using SSL:

public void Main()  
    client = new TcpClient(hostAddress, hostPort); 
    // Create an SSL stream that will close the client's stream. 
    sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); 
    // Do comms here   

private bool ValidateServerCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors)  
{ if (policyErrors == SslPolicyErrors.None) return true; else { // specific error can be found in policyErrors enum; return false; } }

© 2006 - 2014, All work which can be found here is copyrighted to Dave Hogan unless otherwise mentioned.
The information contained on this website is provided for general information only and does not constitute any form of official advice.
Please read the disclaimer for further information.