Skip to content

Get Started

André Secco edited this page Jun 25, 2019 · 5 revisions

Installing

To get started, install the package.

Nuget

Install-Package XLogger.Adapters.MongoDB

.NET CLI

dotnet add package XLogger.Adapters.MongoDB

How to Use

You can use this adapter standalone, but if you are using .NET Core / ASP.NET Core consider using the extensions mentioned here, they make it easier to use XLogger in conjunction with the native Logging API.

Options

The following options are available:

Option Description Default Value
LogLevel Specifies the minimum level to log. Information level (2)
OnDemand Defines whether the adapter will work only on demand (true) or will work also integrated with the ASP.NET Core logging pipeline (false). false
DatabaseUrl The URL of a MongoDB database (Example: mongodb://localhost:27017/DatabaseName) -
CollectionName Name of the collection. logs
Capped Option to create a capped collection. false
MaxSize Max total size in bytes of the created capped collection. 100000000 bytes
MaxDocuments Max number of documents of the created capped collection. 1000 documents

Configuring

The codes below are based on usage in an ASP.NET Core project.

You have two ways to set adapter options:

1) Configuration based on the appsettings.json file

{
  "XLogger": {
    "MongoDB": {
      "LogLevel": 4,
      "OnDemand": false,
      "DatabaseUrl": "mongodb://localhost:27017/MyDatabase",
      "CollectionName": "mylogs"
    }
  }
}
using XLogger;

namespace test
{
    public class Program
    {
        public static void Main(string[] args) =>
            CreateWebHostBuilder(args).Build().Run();

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseXLogger(hub => {
                    hub.AddMongoDB();
                });
    }
}

2) Configuration on add the adapter to hub

using XLogger;

namespace test
{
    public class Program
    {
        public static void Main(string[] args) =>
            CreateWebHostBuilder(args).Build().Run();

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseXLogger(hub => {
                    hub.AddMongoDB(config =>
                    {
                        config.LogLevel = LogLevel.Error;
                        config.OnDemand = false;
                        config.DatabaseUrl = "mongodb://localhost:27017/MyDatabase";
                        config.CollectionName = "mylogs";
                    });
                });
    }
}

Writing logs

To view some ways to write logs in ASP.NET Core, check here.

Output

There are several ways to write the log, this will depend on the object you build. If you wish, you can also rewrite the serializer.

The default record looks like the json below.


{
  "DateTime": ISODate("2019-06-25T00:00:00.000Z"),
  "LogLevel": "Information",
  "Data": "Executed endpoint 'test.Controllers.HomeController.Index (test)'",
  "Exception": {
    "HResult": -2147467261,
    "HelpLink": "https://helplink.com",
    "Message": "Simulated error",
    "Source": "test",
    "StackTrace": "at test.Controllers.HomeController.Index() in c:\\Projects\\test\\Controllers\\HomeController.cs:line 23"
  }
}

Clone this wiki locally