Scott Hanselman

Creating your own Custom PatternLayout, PatternParser, and PatternConvertor with log4net

July 9, '04 Comments [4] Posted in ASP.NET
Sponsored By

Log4Net was ported over from the stunning and wonderful Log4J.  Log4Net supports a large series of Appenders that let you, via a config file, setup a series of destinations for your logging calls.  For example, without recompiling your app, you can send Logging information to a Database, or a File, or ASP.NET Tracing.  It's very flexible, and easier to use (IMHO) than the Microsoft Logging Application Block.

Additionally there's a concept called a Layout that affects the logging data and either formats or adds extra information like the thread id, username, etc.  It's very powerful because your Layout will work for and affect any Appender which makes for nice reuse.  So, for example:

    <!-- A1 is set to be a ConsoleAppender -->    
<appender name="A1" type="log4net.Appender.ConsoleAppender">
<!-- A1 uses PatternLayout -->
<layout type="Corillian.Whatever.Log4NetStuff.CustomPatternLayout, Corillian.Whatever.Log4NetStuff">
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline %G" />
</layout>
</appender>

Here's we're outputing the message, as well as timestamp and thread info. 

I wanted to add my OWN CustomPatternLayout and add additionally %whatever tokens.  This is done all the time in Log4J.

HOWEVER, in the log4net source, there's a function "FinalizeConverter" in PatternParser.cs that you need to override when you derive you own CustomPatternParser from PatterParser.  But, it's not marked virtual.  Crap. 

It makes total sense though, as everything in Java (where this code was ported from) is virtual and can be overridden.  They just forgot to set that log4net 1.2-beta8.  That's why it's beta. :)

So, if you make this change in PatternParser line 237:

protected virtual void FinalizeConverter(char c)

...then you'll be all set to do something like this:

public class CustomPatternLayout : log4net.Layout.PatternLayout
{
  
public SecurityPatternLayout() : this(log4net.Layout.PatternLayout.DEFAULT_CONVERSION_PATTERN){ //whatever }
  public SecurityPatternLayout(string pattern) : base(pattern){ //whatever }
  protected override PatternParser CreatePatternParser(string pattern)  //where we override and return US as a PatternParser
  {
    return new SecurityPatternParser(pattern == String.Empty ? log4net.Layout.PatternLayout.DEFAULT_CONVERSION_PATTERN : pattern);
  }
}

public class SecurityPatternParser : log4net.helpers.PatternParser
{
 
public SecurityPatternParser(string pattern) : base(pattern){}
 protected override void FinalizeConverter(char c)
 {
  if (c == 'G') //where 'G' is your custom token like %G in the format string
 
{
   
AddConverter(new SecurityPatternConvertor()); //Where we add ourselves into the mix, since our token (%G) appears in the format string
 
}
  else base.FinalizeConverter(c);
}

public class SecurityPatternConvertor : log4net.helpers.PatternConverter
{
 
public SecurityPatternConvertor() : base(){}
 
public SecurityPatternConvertor(FormattingInfo formattingInfo) : base(formattingInfo) {}
 
 override protected string Convert(LoggingEvent loggingEvent)
 
{
    
return MessWithThisMessage(loggingEvent.RenderedMessage); //Where we mess with the string however we decide %G should affect it
  
}
}

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

"The dependency whatever.dll cannot be copied to the run directory because it would conflict with the dependancy...

July 9, '04 Comments [4] Posted in ASP.NET | Bugs
Sponsored By

This is always a lovely error to get:

"The dependency whatever.dll cannot be copied to the run directory because it would conflict with the dependancy..."

And again, it all comes down to knowing the places to look.  If you open up the .csproj (or .vbproj) files you'll often see something like:

<Reference
 Name = "Corillian.Voyager.Thingie"
 AssemblyName = "Corillian.Voyager.Thingie"
 HintPath = "..\..\..\build\bin\Corillian.Voyager.Thingie.dll"
/>

And assume that the HintPath is the place that VS.NET looks to resolve the reference.  But, it's only a hint to the IDE.  Often you'll have a situation where a project builds fine on one machine but not other, then the real evil resides in a file that you don't (shouldn't) check into source control and share, the .csproj.user file.  In that file you'll find something like:

<VisualStudioProject>
    <CSHARP LastOpenVersion = "7.10.3077" >
        <Build>
            <Settings ReferencePath = "C:\Dev\VoyagerThingie\source\lib\Voyager\; C:\Dev\VoyagerThingie\source\lib\log4net\; C:\Dev\VoyagerThingie\build\bin\" >

What tends to happen is that folks forget about these files, and as versions change schmutz can creep into these files.  So, when debugging bizarre reference problems, remember to check out the .user file! (or delete it and build it up again)

 

 

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Bad-ass ASP.NET contractor needed

July 9, '04 Comments [0] Posted in ASP.NET
Sponsored By

If you're a ruthlessly competent ASP.NET/C#/*.* developer in the Portland/SW Washington  Area, and you grok most of the technical stuff posted on my blog, email me a resume.
Scott Hanselman - Corillian Corp
scott@corillian.com
--------------------------
Sent from my BlackBerry Wireless Handheld

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

PC Gaming Illogic

July 7, '04 Comments [9] Posted in Musings
Sponsored By

Sorry for this non-technical post, but:

I just got the full version of FarCry - it's 5 CDs.  Yes, FIVE.  For crying out loud, why not ship floppies?  I mean, you'll assume I have a US$400 Video Card, but figure I don't have a US$40 DVD Reader?  Sigh.  I'm on Disc 3 of 5...

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

The Video Card and Pentium Overheating Saga ends...and \Overclocking a ATI Radeon 256Meg 9800 Pro

July 6, '04 Comments [1] Posted in Gaming | Tools
Sponsored By

The Video Card

Well, I've been an NVidia fan for years, and I really thought I'd go for the new NVidia 6800 Ultra.  They've got fantastically stable drivers, dual monitor support, and dual DVI support.  However, it's US$600 and I've got a wife - she'd never go for it.  :)  So, the other option, as was wisely discussed in the comments of my previous post on the subject, is an ATI Radeon 256Meg 9800 Pro.  It's roughly US$299 (half the price) and arguably comparable results for my needs.  Sure, it won't do 100fps but who needs to? 

I wanted Dual-DVI but I'm not willing to pay for it.  There are a few options, but the ATI 9800 Pro is a good compromise.  ONE of it's outputs is DVI, the other is Analog.  So, I run the Dell FP2001 off the DVI at 1600x1200x32 and the Viewsonic VA800 off analog at 1280x1024x32.  It's a very good compromise.  Add in the Tablet PC with Maxivista running at 1400x1080 and that's ~4.7million pixels working for me.

The other reason is that there's an active an happy overclocking community around this card.  The drivers and the card are stable and I've been able to get +11% FPS in most games by overclocking it from 378Mhz(core)/337Mhz(mem) to ~418Mhz/357Mhz.  I can take it higher, but a few artifacts creep in.

Ordinarily I'm not big on overclocking, but in my research I discovered RadLinker, an amazing and free utility for ATI Radeon-based cards.

From his site:

RadLinker is new tweaker/linker for ATI Radeon based graphics cards.  RadLinker features:

  • Absolutely no programs running in the background
  • Does not bloat the registry
  • Per game/program settings
  • Control over process priority
  • Per game/program clock rate settings
  • As many unique game profiles as your computer can hold
  • Custom profiles for all your games

RadLinker allows you to create a "RadLink", a special kind of shell link (a.k.a. shortcut) to your games/programs that allows you to change it's individual settings.  A RadLink will look and act just like a normal shortcut except it's property settings can be custom tailored for your video settings.

RadLinker adds a tab to your Display Properties, but also lets you make custom shortcuts for each game.  In these shortcuts you can specify not only all the details like AntiAliasing, Texture Quality, etc, but the overclocking settings for your Video Card just for that game!  What a fantastic idea.  It works great, and I've been running FarCry and Dungeon Siege at 1600x1200x32 with 4x Anti-Aliasing on and getting 25-30fps!  The difference is NIGHT AND DAY from my NVidia FX5200.  The only problem I've had is with BattleField 1942, apparently there's a problem with the video on Radeons, and I've been unable to get past the menu. 

The Processor

I've also had some trouble with my little P4-3Ghz overheating when rendering DVD Video, squishing TV-Video into WMV or playing games for long periods. 

After toying with the idea over water cooling, I talked to Richard Campbell (an expert on the subject) and decided that was over-kill.  So, I picked up a Zalman CNPS7000A-Cu and installed it.  It was breeze, literally and figuratively.  My average temp is around 42°C and it doesn't get to more than 55-58°C when it's REALLY working.  The case and harddrive are usually around 37°C. 

It also included a Fanmate so I run the Zalman at <2000RPM with the two case fans around 2200RPM.  Currently the loudest thing in the room is the 400W Power Supply fan.  Over all, it's doing OK, and I'm very pleased with the quieter and cooler operation, as well as the stellar 3D and Dual Monitor Support.

This fan also included a free component called "Cooling After" that runs the fans in your computer for a some number of minutes (I set mine to three minutes) after it's turned off.  This is to avoid the sudden spike in temperature that happens after a hot machines shuts off and the fans stop.  It blinks the power-light to let you know it's still cooling down your system.  A nice little piece of insurance.  Kind of like when your car's engine fan continues even after you turn it off.

The Tools/Software

I'm using the following programs to manage all this:

  • RadLinker for ATI Radeon-based cards.  A MUST HAVE.  It's free, but I paypaled the guy $10 just on principle.
  • MBM5 Motherboard Monitor.  Tells you the temperature of the CPU, the other temp monitors on your MB, the RPM of the Fans, and other neat stuff.  The first and the best.  (Sadly Alex Van Kaam, the author, has decided to stop development on MBM as of today, July 6th, 2004.  It's a shame, but 7.5 years on one program is a long time.  Good luck to him!) 
  • DTEMP Hard Drive Temperature Meter - Great little tray icon that not only tells you the temperature of your hard drive (assuming it's a newer one with this ability) but it also monitors the S.M.A.R.T. details and MTBF metrics.
  • SpeedFan.  Programmatically control the speed of your system's fan(s).  This is great if you'd like to get the quietest system possible, while still running the fans at a decent speed.

Thanks again to everyone who helped me with all this, and for the ATI 9800 Pro suggestion - also running an LCD with DVI versus Analog really is noticeable, especially when the native resolution of the LCD Is 1600x1200.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.