Skip to content

Logging component for .NET Core with nice APIs for developers to use.

License

Notifications You must be signed in to change notification settings

cosmos-loops/cosmos-logging

Repository files navigation

COSMOSLOOPS/Cosmos.Logging

Logging component for .NET Core with nice APIs for developers to use. Cosmos.Logging is support for structured logging message in several kinds of .NET applications.

This repository belongs to Cosmosloops Labs..

Nuget Packages

Usage

Install the package

Install-Package Cosmos.Logging

Install the specific sink packages, renderer packages or extension packages that you need.

Work in console

Install console package first:

Install-Package Cosmos.Logging.RunsOn.Console

then: (in this case we integrated NLog sink)

static void Main(string[] args)
{
    //load configuration info
    var root = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", true, true)
        .Build();

    //initialize logger
    LOGGER.Initialize(root).RunsOnConsole().AddNLog().AddSampleLog().AllDone();

    //submit log manually
    var logger1 = LOGGER.GetLogger<Program>(mode: LogEventSendMode.Manually);
    logger1.LogWarning("hello, {$Date} {$MachineName}");
    logger1.SubmitLogger();

    //or submit log automatically
    var logger2 = LOGGER.GetLogger<Program>();
    logger2.LogWarning("world");

    //get a future logger
    var logger3 = LOGGER.GetLogger<Program>().ToFuture();

    //or convert a normal to future logger
    var logger4 = logger2.ToFuture();

    //or get a future logger directly
    var logger5 = FUTURE.GetFutureLogger<Program>();

    //and how to use future logger via a nice fluent api
    logger5.SetLevel(LogEventLevel.Warning)
        .SetMessage("future log===> Nice {@L}")
        .SetTags("Alex", "Lewis")
        .SetParameter(new {L = "KK2"})
        .SetException(new ArgumentNullException(nameof(args)))
        .Submit();

    //or you can use a optional-parameters-style api in your Application-Framework
    logger5.UseFields(
        Fields.Level(LogEventLevel.Warning),
        Fields.Message("future log===> Nice {@L}"),
        Fields.Tags("Alex", "Lewis"),
        Fields.Args(new {L = "KK3"}),
        Fields.Exception(new ArgumentNullException(nameof(args)))).Submit();
}

Work with Microsoft ASP.NET Core Mvc

Install aspnetcore package first:

Install-Package Cosmos.Logging.RunsOn.AspNetCore

then write code in Startup.cs: (in this case, we integrated SqlSugar ORM)

public void ConfigureServices(IServiceCollection services)
{
    //...

    services.AddCosmosLogging(Configuration, config => config
            .ToGlobal(o => o.UseMinimumLevel(LogEventLevel.Information))
            .AddDatabaseIntegration(o => o.UseSqlSugar(sugar => sugar.UseAlias("Everything", LogEventLevel.Verbose)))
            .AddSampleLog());

    //...
}

finally, just to writing your code:

public class HomeController : Controller
{
    private readonly ILoggingServiceProvider _loggingProvider;

    public HomeController(ILoggingServiceProvider loggingProvider) {
        _loggingProvider = loggingProvider ?? throw new ArgumentNullException(nameof(loggingProvider));
    }

    // GET
    public IActionResult Index() {
        var log = _loggingProvider.GetLogger<HomeController>();
        log.LogInformation("Nice @ {$Date}");
        return Content("Nice");
    }
}

Work with Microsoft ASP.NET Mvc

Install aspnet package first:

Install-Package Cosmos.Logging.RunsOn.AspNet

or (if you use Autofac as your default IoC container)

Install-Package Cosmos.Logging.RunsOn.AspNet.WithAutofac

then

//in Global.asax.cs
public class Global : HttpApplication
{
    //...
    this.RegisterCosmosLogging(s => s.ToGlobal(c => c.UseMinimumLevel(LogEventLevel.Verbose)).AddSampleLog());
}

Thanks

People or projects that have made a great contribution to this project:

  • The next one must be you

Organizations and projects

  • The next one must be yours

License

Member project of Cosmosloops Labs..

Apache License 2.0