Skip to main content

Posts

Showing posts with the label C#

A simple IIS HTTP module to log request headers and post data

This is a simple http module that hijacks the incoming request and logs it to a file. Using this module prevents the application from getting the post data because the input stream can only be read once. With .NET 4.0 the module could be much smaller, and could allow the application to read the data. To compile this, create a new "Class Library" application called PostDataLogger. Most of this class is pieced together from stolen code (stack overflow, etc.) using System; using System.IO; using System.Web; /// /// Dump PostDataLogger.DLL in your bin, and put this in your web.config inside of /// /// /// /// public class SimpleLogger : IHttpModule { private HttpApplication _ctx; public void Init(HttpApplication context) { _ctx = context; context.BeginRequest += new EventHandler(Context_BeginRequest); } void Context_BeginRequest(object sender, EventArgs e) { string GUID = Guid.NewGuid().ToString(); string filename = @"d:\temp\adeber...

How to log C# exception information including all inner exceptions

/// /// Logs a type and message for and exception (and all inner exceptions) on multiple log lines. /// Lines all contain a unique ID so they can be found if other lines get between them. /// /// Method name, or whatever you want. /// Custom message for the first log line /// Exception to log public static void LogException(string tag, string message, Exception ex) { string logItemId = Guid.NewGuid().ToString("N"); Log(tag, false, logItemId + " " + message); Log(tag, false, logItemId + " Exception: " + ex.GetType().Name + ": " + ex.Message); Exception Inner = ex.InnerException; int innerNumber = 1; while( Inner != null ) { Log(tag, false, logItemId + " Inner Exception " + innerNumber + " " + Inner.GetType().Name + ": " + Inner.Message); Inner = Inner.InnerException; innerNumber++; } }

Parse and organize command line options using C#

This class will parse, organize and collect command line options. I couldn't find anything like it, so I wrote one. This works all the way down to .NET 1.1 if you are lucky enough to still be using that. Save this as ArgDictionary.cs, change the namespace as needed, and feel free to use it in your projects. using System; using System.Collections.Generic; namespace Utilities { /// /// Implements a Dictionary object for handling command line options. /// Supports options with prefixes -, --, or /. /// Supports Boolean, String, or Int32 option values. /// String options are in the following format: /// /stringOption value /// Int32 options are in the following format: /// /intOption 200 /// Boolean options are in the following format: /// /boolOption /// NOTE: if the Boolean option is present in the command line, it is true. /// /// Supports both a long and short version for each option (e.g. /a and /Account /// can be mapped to the same value). /// NOTE: When ...

How to delete a file in C# that might be in use

int retries = 5; while (retries-- >= 0) { try { retries = -1; System.IO.File.Delete(@"C:\deleteme.txt"); } catch (System.IO.IOException ex) // "The specified file is in use." { // File is locked? Sleep and try again. System.Threading.Thread.Sleep(100); // Wait 100ms if (retries >= 0) continue; // go back to while loop throw ex; // give up } } This will also work for writing to a file, etc. This should be enclosed in a try/catch block to catch the other exceptions thrown by the System.IO.File.Delete method.

List all IP addresses bound to a box with C#

RegistryKey adaptersRegKey = Registry.LocalMachine; adaptersRegKey = adaptersRegKey.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Adapters"); String[] adapterGuids = adaptersRegKey.GetSubKeyNames(); foreach (String giud in adapterGuids) { RegistryKey interfaceRegKey = Registry.LocalMachine; interfaceRegKey = interfaceRegKey.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" + giud); try { Console.WriteLine("Checking: " + giud); Object ipObject = interfaceRegKey.GetValue("IPAddress"); Array ipArray = (Array)ipObject; foreach (string ip in ipArray) { Console.WriteLine("Found IP: " + ip); } interfaceRegKey.Close(); } catch (NullReferenceException) { } } adaptersRegKey.Close();