ToolsPack.Log4net 3.1.0

dotnet add package ToolsPack.Log4net --version 3.1.0
NuGet\Install-Package ToolsPack.Log4net -Version 3.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ToolsPack.Log4net" Version="3.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ToolsPack.Log4net --version 3.1.0
#r "nuget: ToolsPack.Log4net, 3.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ToolsPack.Log4net as a Cake Addin
#addin nuget:?package=ToolsPack.Log4net&version=3.1.0

// Install ToolsPack.Log4net as a Cake Tool
#tool nuget:?package=ToolsPack.Log4net&version=3.1.0

ToolsPack.Log4net

Checkout also the ToolsPack.NLog package. I personally think that NLog is better than Log4Net..

LogQuickConfig

In a Unit test project, or a temporary console application, you donnot have to configure the log4net.config any more.

Call

LogQuickConfig.SetupConsole();

or

LogQuickConfig.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();
		//or Log4NetQuickSetup.SetUpFile("mytest.log");
	}

	[TestMethod]
	public void DisplayTest()
	{
		Log.Info("it will display to the Console");
		//or to the file if you use SetUpFile()
	}
}

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

ElapsedTimeWatcher ⇐= Deprecated, use the ToolsPack.Logging.ElapsedTimeLogger instead

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.Debug("step 2");
			...
		    etw.Info("Step 3)");
		    ...
		} //"sum up log" is displayed here
	}
}
  • 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 object is disposed.
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

Customize Start Context and End context

var etw = ElapsedTimeWatcher.Create(Log, "foo", "Start_context", "End_context");

will give

22:56:59,866 [DEBUG] Begin Start_context
22:56:59,970 [INFO ] foo - 102350 mcs - step 1
22:57:00,144 [DEBUG] foo - 173295 mcs - step 2
22:57:00,259 [INFO ] foo - 114036 mcs - Step 3)
22:57:00,452 [INFO ] End End_context : Total elapsed 585436 mcs

We often display the parameter of the functions in the "Start context". Example:

public void process(string val, bool useCache)
{
	var context = string.Format("process(val={0}, useCache={1})", val, useCache);
	using (var etw = ElapsedTimeWatcher.Create(Log, "process", context))
	{
	    ...
	    etw.Info("step 1");
	    ...
	    etw.DebugFormat("step 2");
		...
	    etw.Info("Step 3)");
	    ...
	} //"sum up log" is displayed here
}

will give

22:56:59,866 [DEBUG] Begin process(val=Lorem ipsum, useCache=true)
22:56:59,970 [INFO ] process - 102350 mcs - step 1
22:57:00,144 [DEBUG] process - 173295 mcs - step 2
22:57:00,259 [INFO ] process - 114036 mcs - Step 3)
22:57:00,452 [INFO ] End process(val=Lorem ipsum, useCache=true) : Total elapsed 585436 mcs
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.0 100 2/8/2024
3.0.4 8,598 5/14/2020
3.0.3 462 5/14/2020
3.0.2 499 4/13/2020
3.0.1 601 4/4/2020
3.0.0 410 7/5/2022
2.0.2 588 1/3/2020
2.0.1 521 1/3/2020
2.0.0 594 5/31/2019
1.0.1.2 1,060 6/7/2017
1.0.1.1 1,557 11/19/2015
1.0.1 1,362 11/15/2015
1.0.0 1,509 11/13/2015