SWCLogging 1.0.4

dotnet add package SWCLogging --version 1.0.4
NuGet\Install-Package SWCLogging -Version 1.0.4
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="SWCLogging" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SWCLogging --version 1.0.4
#r "nuget: SWCLogging, 1.0.4"
#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 SWCLogging as a Cake Addin
#addin nuget:?package=SWCLogging&version=1.0.4

// Install SWCLogging as a Cake Tool
#tool nuget:?package=SWCLogging&version=1.0.4

Why?

The idea of building this library coined when I was going through features of .Net core and we got to know about ILogger interface. I got to know about the mechanism that there is a way to write my own logging provider and !voila.

I am always fascinated about the speed of the Elasticsearch which is used for log analytics, full-text search, security intelligence, business analytics, and operational intelligence use cases. This is based on Apache Lucene. There is another logging library present called ELMAH which used to be free but now it is subscription based library.

I have tried to build SWCLogging which is also based on Apache Lucene which provides you a way to log and search capability in its built in search page.

What?

SWCLogging is a logging library which helps us to not only in logging but also provides us an in built user interface to search and analyze the log.

How?

I have used the capability of Apache Lucene in the library and created a logging provider which in turn could be used as a logging provider in any .Net Core application. This is very easy to use and integrate.

Security

The built in page that this library has returns the whole HTML of the page in string format. Now it is upto you to get this HTML and place in your applicaton's page(cshtml). So whoever has right to see this particular page those will have access to the loging page. Here security is completely in your control.

Screenshots

Log

log search

Logging Verbosity

It takes care of logging verbosity also mentioned in the appsettings.json file. Here the "Default" cateogry is "Information" it denotes minimum label to log for the category. "Microsoft" cateogry is "Critical" it denotes minimum label to log for the category.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Critical",
      "Microsoft.Hosting.Lifetime": "Critical"
    }
  }
}

Integration

  1. Install SWCLogging
NuGet\Install-Package SWCLogging -Version 1.0.4
    • Create a controller called SWCLController - Add two namespaces. - Add a constructor with parameter Ilogger and ILoggerProvider which will automatically be initialized by dependency injection.
using Microsoft.Extensions.Logging;
using SWCLoggingLibrary;
public class SWCLController : Controller
{
    private readonly ILogger<SWCLController> _logger;
    private readonly ILoggerProvider _loggerProvider;
        
    public SWCLController(ILogger<SWCLController> logger, ILoggerProvider loggerProvider)
    {
        _logger = logger;
        _loggerProvider = loggerProvider;
    }
}
    • Add the Index method to get the search page - Add a post method "GetLogs" which would help us fetching the logs on the search page based on our search criteria.
public IActionResult Index()
{
    SWCLogging swclog = SWCLogging.GetInstance((SWCLoggingProvider)_loggerProvider);
    ViewBag.Data = swcl.GetSearchPage();
    return View();
}

[HttpPost]
public IActionResult GetLogs(SWCSearchRequest model)
{
    SWCLogging swclog = SWCLogging.GetInstance((SWCLoggingProvider)_loggerProvider);
    var res = swclog.SearchLogs(model);
    return Json(new { result = res });
}
    • Add an index view - Set the Layout to null (do not forget this step) - Place the HTML on the page
@{
    Layout = null;
}
@Html.Raw(ViewBag.Data)
  1. Add SWCLogging as a logging provider in Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
    .ConfigureLogging(logging =>
    {
        logging.ClearProviders();
        logging.AddSWCLogging();
    })
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseStartup<Startup>();
    });

Usage

This is very easy to use. To use the logging we only need to use the _logger object. Here are all the methods for different log level. Every method have different parameters which could be used to add more information in the log.

_logger.LogInformation("This is log information")
_logger.LogDebug("Home screen Starting up");
_logger.LogError("This is error log of home screen.");
_logger.LogTrace("This is trace log");
_logger.LogCritical(ex,"This is critical log");
_logger.LogWarning("Just a warning");

We also have a way to define scope of logs. For example, if we want to search logs for specific method/functionality then we may define scope and through this we can find all the logs which falls in this scope. In below example we can find all logs related to this functionality through the text "Payment_09843" as this text is defined in the scope. And you will also find this text in each log with key scope.

using (_logger.BeginScope("Payment_09843"))
{
    _logger.LogInformation("Payment module starts.")
    _logger.LogWarning("Remarks not found.");
    _logger.LogCritical("Some error has occurred at step 5");
    _logger.LogInformation("Payment module end.")
}

Where

Since this is based on Apache Lucene, it creates logs in a folder(swclog) in bin folder of your project. If you need to cleanup all the logs, you only need to empty this folder.

Launch

To launch the search page you only need to access the Index action from SWCLController

<url>/SWCL | localhost:<port number>/SWCL

License

MIT

Enjoy!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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
1.0.4 627 5/5/2023
1.0.3 645 4/19/2023
1.0.2 800 1/11/2023

- Ability to share search criteria so that same view could be executed on another machine
     - Constructing url for every log so that it could be easily shareable
     - Current version information is available on mouse over of copyright information
     - Corrected project url
     - Ability to execute search on click of legends