This guide describes how to enable and configure logging for Flexmonster Data Server as a DLL.
Note Instructions from this guide are for the projects based on ASP.NET Core version 3.1 or ASP.NET versions from 5 to 6.
By default, the Data Server relies on the project-wide logging settings. If logging is disabled in your project, the Data Server does not log. If logging is enabled, the Data Server logs at the default log level and higher.
The Data Server does not require any specific configurations or changes to its code to start logging. It automatically logs to the used logging provider.
It depends on the logging provider used in your project. If a built-in provider is used, logs are displayed in Visual Studio or in the console. If you are using a third-party or custom provider, the Data Server’s logs are stored in a location specified in the provider’s configurations.
Ways to configure log levels depend on a logging provider used in your project:
With built-in logging providers, you can configure log levels for the Data Server via the configuration file (e.g., appsettings.json
). In the file, specify the Flexmonster.DataServer.Core
category under the LogLevel
property of the used provider:
{
"Logging": {
"Debug": {
"LogLevel": {
// Other settings
"Flexmonster.DataServer.Core": "Warning",
// Other settings
}
}
}
}
In the above code snippet, Flexmonster.DataServer.Core
is set to "Warning"
. This means you will see the Data Server’s logs at the level Warning
and higher.
Learn more about configuring built-in providers: Configure logging.
If your project uses one of the supported third-party providers, log levels for the Data Server should be configured within that provider. Use the Flexmonster.DataServer.Core
category to configure log levels.
The same applies to custom logging providers that implement the ILoggerProvider interface.
If needed, you can customize the Data Server's logging by creating your implementation of the IFlexmonsterLogger
interface:
Step 1. In your project, create a C# class (e.g., FlexmonsterLogger.cs
).
Step 2. Open the created file and import Flexmonster.DataServer.Core.Logger
:
using Flexmonster.DataServer.Core.Logger;
Step 3. Implement the IFlexmonsterLogger
interface by implementing the LogDebug
, LogInformation
, LogWarning
, LogError
, and LogCritical
methods.
Here's a sample implementation:
using Flexmonster.DataServer.Core.Logger;
using Microsoft.Extensions.Logging;
using System;
namespace FDS_Project
{
public class FlexmonsterLogger<T> : IFlexmonsterLogger<T>
{
/* You can use any logger type aside from ILogger */
private readonly ILogger<T> _logger;
public FlexmonsterLogger(ILogger<T> logger)
{
_logger = logger;
}
public void LogDebug(string message, params string[] args)
{
_logger?.LogDebug(message, args);
}
public void LogInformation(string message, params string[] args)
{
_logger?.LogInformation(message, args);
}
public void LogWarning(string message, params string[] args)
{
_logger?.LogWarning(message, args);
}
public void LogError(string message, params string[] args)
{
_logger?.LogError(message, args);
}
public void LogCritical(string message, params string[] args)
{
_logger?.LogCritical(message, args);
}
}
}
Step 4. In the Program.cs
file, add the following lines of code to register your logger implementation:
using Flexmonster.DataServer.Core.Logger; var builder = WebApplication.CreateBuilder(args); // Other configurations builder.Services.AddFlexmonsterApi(); /* Note that the logger registration should always go after the builder.Services.AddFlexmonsterApi() line */ builder.Services.AddSingleton(typeof(IFlexmonsterLogger<>), typeof(FlexmonsterLogger<>)); // Other configurations
Now the Data Server will use your custom logger implementation.
You may be interested in the following articles: