August 22, 2009

One Day of Vacation

Finally, a nice day of no work and time spent swimming and relaxing. Recharged the batteries and ready for work next week.

August 21, 2009

LogParser Examples in c#

Seems like many people are downloading my examples in c# for logparser. I will work on some more examples and post them up for you guys.

August 20, 2009

AzMan Bulk Import/Export Tool V2

Some people have been asking me for this. I have been a bit busy lately on other things...but I promise I will get to it soon.

August 19, 2009

Backup files/folder structure with Powershell

I was asked to look at a network share and backup the share and folder structure exactly how it is laid out to another location. I am sure this probably could be written with less code, but here is what I did.
$sourceDir = '\\MySrcShare\d$\\MySrcFolder'
$backupDir = 'K:\DestShare\Bkp'

function NormalizePath($path)
{
if($null -eq $path)
{
return $path
}

[System.IO.Path]::GetDirectoryName($path + "\")
}

$sourceDir = NormalizePath($sourceDir)
$backupDir = NormalizePath($backupDir)

$backupDir = [System.IO.Path]::Combine($backupDir, [System.IO.Path]::GetFileName($sourceDir))


dir $sourceDir -Recurse | % {

$relativePath = $_.FullName.Substring($sourceDir.length + 1)
$dest = [System.IO.Path]::Combine($backupDir, $relativePath)

if(Test-Path $_.FullName -PathType container)
{
New-Item -Path $dest -ItemType Directory -ErrorAction SilentlyContinue
}
else
{
$ext = [System.IO.Path]::GetExtension($_);

if(($ext.ToLower() -eq '.xml') -or ($ext.ToLower() -eq '.config'))
{
Copy $_.FullName -Destination $dest
}
}
}
This will copy the folder structure of source (including all sub-directories) to the destination and it will also copy *.config and *.xml files to their respective folders on the destination drive.

Anyone know how to tweak this or make it smaller?

August 18, 2009

Audit using Log4Net

I had taken a web service off the hands of someone and it had no logging. I needed a quick way to log everything from every method in the code. I also needed to pull the user executing the method. In most cases this would be simple, but not in this webservice...I figured I would read it from a cookie that we had. Nothing would be easy as I knew I had to place statements in some methods, but I wanted to at least make a reusable class.

I used Log4Net and have the log spit out xml to make it easy to import or work with. The code is fairly simple to use:
//In the try
Auditor.Audit.Method(this.Context, "MethodName", new object[] { request });

//in the catch:
Auditor.Audit.Method(this.Context, "MethodName", new object[] { request }, ex);
The code that does the simple audit:
    public class Audit
{
private static AuditorCore m_core = new AuditorCore();
private static bool enabled = false;

static Audit()
{
log4net.Config.XmlConfigurator.Configure();

try
{
enabled = bool.Parse(ConfigurationSettings.AppSettings["Audit.Enabled"]);
}
catch(Exception)
{
enabled = false;
}
}

public static void Method(HttpContext context, string methodName, IEnumerable arguments, Exception exception)
{
if (enabled)
{
m_core.Method(context, methodName, arguments, exception);
}
}
public static void Method(HttpContext context, string methodName, IEnumerable arguments)
{
if (enabled)
{
m_core.Method(context, methodName, arguments);
}
}
}


The core class:

internal class AuditorCore
{
private log4net.ILog log = log4net.LogManager.GetLogger("Audit");

private Dictionary m_serializers =
new Dictionary();

public void Method(HttpContext context, string methodName, IEnumerable arguments)
{
Method(context, methodName, arguments, null);
}

public void Method(HttpContext context, string methodName, IEnumerable arguments, Exception exception)
{
lock (m_serializers)
{
log.Info(string.Format("",
methodName,
GetUserId(context)));

foreach (object argument in arguments)
{
string xmlText = SerializeToXmlString(argument);
log.Info(xmlText);
}

if (exception != null)
{
log.Error("");
}

log.Info("
");
}
}

private string SerializeToXmlString(object obj)
{
if(obj == null)
{
return "";
}

// Use below object to avoid namespaces in result xml.
XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add("", "");

XmlSerializer serializer = GetSerializer(obj.GetType());
using (StringWriter stringWriter = new StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
{
serializer.Serialize(xmlWriter, obj, xmlnsEmpty);
}

// Strip out the header.
string xmlText = stringWriter.ToString();
xmlText = xmlText.Substring(xmlText.IndexOf('>') + 1);

return xmlText;
}
}

private XmlSerializer GetSerializer(Type type)
{
if (!m_serializers.ContainsKey(type))
{
m_serializers[type] = new XmlSerializer(type);
}

return m_serializers[type];
}

private string GetUserId(HttpContext context)
{
if (context.Request.Cookies["MyCookie"] != null)
{
return context.Request.Cookies["MyCookie"]["userid"];
}
else
{
return null;
}
}
}

August 17, 2009

Web 2.0 Name

I need a web 2.0 name...so hard to come up with!

August 16, 2009

IRC Chat Bot in Powershell

I came across this today...very cool.