CodeWithSaar.Extensions.Logging.File 1.0.0

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

// Install CodeWithSaar.Extensions.Logging.File as a Cake Tool
#tool nuget:?package=CodeWithSaar.Extensions.Logging.File&version=1.0.0                

CodeWithSaar.Extensions.Logging.File

CodeWithSaar.Extensions.Logging.File is a very easy to use, lightweight file logger provider implementation for Microsoft.Extensions.Logging. Start to use it with ASP.NET Core by one line of code.

Getting Started

  • Add NuGet Package:

    dotnet add package CodeWithSaar.Extensions.Logging.File --prerelease
    
  • In ASP.NET Core project, enable the logging provider:

    ...
    // Add services to the container.
    builder.Services.AddLogging(loggingBuilder =>{
        loggingBuilder.AddFile();
    });
    ...
    

    Check out the example project for WebAPI.

  • Run the application and see the log output to output.log, and you are expected to see log entries like this:

    2022-05-01T15:01:06.6811783-07:00 dbug: Microsoft.Extensions.Hosting.Internal.Host[1] Hosting starting
    2022-05-01T15:01:09.7265572-07:00 info: Microsoft.Hosting.Lifetime[14] Now listening on: https://localhost:7038
    2022-05-01T15:01:09.7269258-07:00 info: Microsoft.Hosting.Lifetime[14] Now listening on: http://localhost:5125
    2022-05-01T15:01:09.7281242-07:00 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down.
    2022-05-01T15:01:09.7283214-07:00 info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development
    2022-05-01T15:01:09.7283445-07:00 info: Microsoft.Hosting.Lifetime[0] Content root path: D:\Repos\CodeWithSaar.Extensions.Logging\examples\WebAPIExample\
    2022-05-01T15:01:09.7283522-07:00 dbug: Microsoft.Extensions.Hosting.Internal.Host[2] Hosting started
    2022-05-01T15:01:13.9569295-07:00 info: Microsoft.Hosting.Lifetime[0] Application is shutting down...
    2022-05-01T15:01:13.9580170-07:00 dbug: Microsoft.Extensions.Hosting.Internal.Host[3] Hosting stopping
    2022-05-01T15:01:13.9705049-07:00 dbug: Microsoft.Extensions.Hosting.Internal.Host[4] Hosting stopped
    

Benchmark Result

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
Intel Core i7-9700 CPU 3.00GHz, 1 CPU, 8 logical and 8 physical cores
.NET SDK=6.0.300-preview.22204.3
  [Host]     : .NET 6.0.3 (6.0.322.12309), X64 RyuJIT
  DefaultJob : .NET 6.0.3 (6.0.322.12309), X64 RyuJIT
Method Mean Error StdDev
Log 750.3 ns 14.14 ns 13.22 ns

Example projects

Customization

Set a different file name

There are generally 2 ways to set log configurations.

  • By code:

    builder.Services.AddLogging(loggingBuilder =>
    {
        loggingBuilder.AddFile(opt => opt.OutputFilePath = "newfilename.log");
    });
    
  • By configuration file (Typically, appsettings.json):

    {
        // General logging
        "Logging": {
            "LogLevel": {
                "Default": "Debug"
            },
            // File provider overwrites
            "FileProvider":
            {
                // Output to output.v2.log file
                "OutputFilePath": "output.v2.log",
                // Specify a different logging level
                "LogLevel":{
                    "Default": "Information"
                }
            }
        }
    }
    

Set different logging levels

Logging levels could be set in through appsettings.json / appsettings.Development.json or whatever the configuration providers in your project.

It would take the default settings like this:

    {
        // Logging Information or above to file
        "Logging": {
            "LogLevel": {
                "Default": "Debug"
            }
        }
    }

Or it could be set to it's own:

{
    // Default logging level of Debug
    "Logging": {
        "LogLevel": {
            "Default": "Debug"
        },
        "FileProvider":
        {
            // Output to output.v2.log file
            "OutputFilePath": "output.v2.log",
            // Specify a different logging level of Information for logs output to the file.
            "LogLevel":{
                "Default": "Information"
            }
        }
    }
}

Other customizations

There are more options than just the filename. All supports the settings from either the delegate in code or the configuration providers (like it in appsettings.json). Refer to FileLoggerOptions.cs for more properties to be customized like: ShowFullCategoryName, TimestampFormat, etc.

Others

Please star this repo if you like this repo 😃.

Want to know how is this built? Check out the following videos:

Issues

  • If you want features or encountered any problem, file an issue.

Disclosure

  • I am current working for Microsoft.
  • This is NOT a Microsoft project.
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 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 was computed. 
.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
1.0.0 727 5/9/2022
1.0.0-beta6 242 5/2/2022
1.0.0-beta5 237 4/19/2022
1.0.0-beta4 208 4/12/2022
1.0.0-beta2 186 4/12/2022
1.0.0-beta1 185 4/12/2022