Skip to content
DUONG Phu-Hiep edited this page Nov 16, 2015 · 16 revisions

My collection of many small useful code-snippet .net, wrapping in reusable library with unobtrusive dependencies. Some of the most-used library are published on nuget.

https://www.nuget.org/profiles/duongphuhiep

ToolsPack.Displayer

  • No dependencies
  • Use to display some C# object.

ArrayDisplayer

  • Know to convert a IEnummerable to string in order do display in a log message
var arr = new string[1000] {"item1".."item1000"};
arr.Display().SeparatedBy("; ").MaxItems(4)

gives

{ item1; item2; item3; item4; ..and 996 (of 1000) more }
  • if the some of the items is very long to display in the log file
var arr = new string[1000] {"Lorem ipsum kidda foom", "item2".."item1000"};
arr.Display().MaxItems(4).MaxItemLength(10)

gives

{ [[Lorem...]], item2, item3, item4, ..and 996 (of 1000) more }
  • Fast performance it only iterate neccessary items once (complexity O(N))
  • see more functionalities in code and test

StopwatchDisplayer

convert Stopwatch to string

Stopwatch sw;
Console.WriteLine(sw.DisplayMili()); //get the display string in mili seconds "103 ms"
Console.WriteLine(sw.DisplayMicro()); //get the display string in micro seconds "103,000 mcs"
Console.WriteLine(sw.Display()); //automaticly choose a time unit (day, hour, minute, seconde..) to display

ToolsPack.Log4net

Log4NetQuickSetup

In a Unit test project, or a quick console temporary application to try things, you donnot need to configure the log4net.config.

Call

Log4NetQuickSetup.SetUpConsole();

or

Log4NetQuickSetup.SetUpFile("my_small_app.log");

it will setup a typical log4net appender so that you can use them in your test application. Example:

[TestClass]
public class ArrayDisplayerTests
{
	private static readonly ILog Log = LogManager.GetLogger(typeof (ArrayDisplayerTests));

	[ClassInitialize]
	public static void SetUp(TestContext testContext)
	{
		Log4NetQuickSetup.SetUpConsole();
	}

	[TestMethod]
	public void DisplayTest()
	{
		Log.Info("Hello it will display to the Console");
	}
}

See also code-snippet to quickly configure log4net in a C# project

ConfigReader

use it to read app.config

Example app.config of your application

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<appSettings>
		<add key="connectionString" value="Server=localhost;Database=foo"/>
		<add key="activePingService" value="true"/>
		<add key="pollIteration" value="100"/>
	</appSettings>
</configuration>

You can read these value in your C# application

ConfigReader.Read<string>("connectionString", "a default value if config not found");
ConfigReader.Read<bool>("activePingService", false);
ConfigReader.Read<int>("pollIteration", -1);

ElapsedTimeWatcher

Micro-benchmark a part of code to investigate on performance

class MyCalculator 
{
	private static readonly ILog Log = LogManager.GetLogger(typeof(MyCalculator));

	public void Process()
	{
		using (var etw = ElapsedTimeWatcher.Create(Log, "blockCodeName"))
		{
		    ...
		    etw.Info("step 1");
		    ...
		    etw.DebugFormat("step 2");
			...
		    etw.Info("Step 3)");
		    ...
		}
	}
}
  • The etw wrap the usual logger Log, we use etw to log message instead of the usual Log
  • the blockCodeName is repeated in the start of each log message, so that we can filter log message by "blockCodeName"
  • Each log message will display the elapsed time (in micro-second) since the last log message.
  • A sum up log will display the total elapsed time (in micro-second) when the etw is destroyed
22:56:59,866 [DEBUG] Begin blockCodeName
22:56:59,970 [INFO ] blockCodeName - 102350 mcs - step 1
22:57:00,144 [DEBUG] blockCodeName - 173295 mcs - step 2
22:57:00,259 [INFO ] blockCodeName - 114036 mcs - Step 3)
22:57:00,452 [INFO ] End blockCodeName : Total elapsed 585436 mcs

Auto Jump Log Level

var etw = ElapsedTimeWatcher.Create(Log, "checkIntraday").InfoEnd().AutoJump(150, 250).AutoJumpLastLog(500, 1000)
  • The log level will auto jump to INFO if the elapsed time exceeds 150 ms
  • The log level will auto jump to WARN if the elapsed time exceeds 250 ms
  • The above sum up log will switch to INFO if the total elapsed time exceeds 500 ms
  • The above sum up log will switch to WARN if the total elapsed time exceeds 1 sec