Skip to content

Includes Cosmos.Standard, Cosmos.Encryption and Cosmos.Logging for CosmosLoops Programme, one member of StarrySky Headquarters.

License

Notifications You must be signed in to change notification settings

alexinea/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 COSMOS LOOPS PROGRAMME.

Nuget Packages

Package Name Version Downloads
Cosmos.Logging
Cosmos.Logging.RunsOn.AspNet
Cosmos.Logging.RunsOn.AspNet.WithAutofac
Cosmos.Logging.RunsOn.AspNetCore
Cosmos.Logging.RunsOn.Console
Cosmos.Logging.RunsOn.NancyFX
Cosmos.Logging.RunsOn.NancyFX.WithAutofac
Cosmos.Logging.RunsOn.ZkWeb
Cosmos.Logging.Extensions.Microsoft
Cosmos.Logging.Extensions.EntityFramework
Cosmos.Logging.Extensions.EntityFrameworkCore
Cosmos.Logging.Extensions.NHibernate
Cosmos.Logging.Extensions.FreeSql
Cosmos.Logging.Extensions.SqlSugar
Cosmos.Logging.Extensions.PostgreSql
Cosmos.Logging.Extensions.Exceptions
Cosmos.Logging.Extensions.Exceptions.EntityFramework
Cosmos.Logging.Extensions.Exceptions.EntityFrameworkCore
Cosmos.Logging.Extensions.Exceptions.MySql
Cosmos.Logging.Extensions.Exceptions.MySqlConnector
Cosmos.Logging.Extensions.Exceptions.Oracle
Cosmos.Logging.Extensions.Exceptions.PostgreSql
Cosmos.Logging.Extensions.Exceptions.Sqlite
Cosmos.Logging.Extensions.Exceptions.SqlServer
Cosmos.Logging.Extensions.NodaTime
Cosmos.Logging.Sinks.AliyunSls
Cosmos.Logging.Sinks.JdCloud
Cosmos.Logging.Sinks.TencentCloudCls
Cosmos.Logging.Sinks.Exceptionless
Cosmos.Logging.Sinks.TomatoLog
Cosmos.Logging.Sinks.Log4Net
Cosmos.Logging.Sinks.NLog
Cosmos.Logging.Sinks.Console
Cosmos.Logging.Sinks.File
Cosmos.Logging.Renderers.Environment
Cosmos.Logging.Renderers.Process

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


License

Member project of COSMOS LOOPS PROGRAMME.

Apache 2.0 License

About

Includes Cosmos.Standard, Cosmos.Encryption and Cosmos.Logging for CosmosLoops Programme, one member of StarrySky Headquarters.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.5%
  • Other 0.5%