Developer forum

Forum » Development » How to enable custom file logging for middleware in DW10 (in Cloud Staging/Prod)

How to enable custom file logging for middleware in DW10 (in Cloud Staging/Prod)

Ruwan Dissanayake
Reply

We are currently running a Dynamicweb 10 solution on Dynamicweb Cloud and are having trouble getting custom logs to write to the file system (/Files/System/Log) in our Staging and Production environments.

The Goal:
We have implemented a custom GeoRoutingMiddleware to handle market-specific redirects. We want to log these routing decisions so we can monitor them in staging and production.

The Implementation:
Currently, we are using the Dynamicweb.Logging.LogManager.Current.GetLogger("MarketRouting") to generate these logs. We test-fired the logger in both Program.cs and our middleware:

In Program.cs:

// ... [Standard DW10 app building] ...

app.UseMiddleware<GeoRoutingMiddleware>();
app.UseDynamicweb();

// Test logs
var geoLogger = Dynamicweb.Logging.LogManager.Current.GetLogger("MarketRouting");
geoLogger.Debug("Debug - Application started.");
geoLogger.Info("Info - Application started.");
geoLogger.Warn("Warn - Application started.");
geoLogger.Error("Error - Application started");

app.Run();  

 

In GeoRoutingMiddleware.cs:  

// ...
private DwLogger Logger => _logger ??= Dynamicweb.Logging.LogManager.Current.GetLogger("MarketRouting");

public async Task InvokeAsync(HttpContext context)
{
    if (context.Request.Path != "/")
    {
        await _next(context);
        return;
    }

    Logger.Warn("Market routing middleware handling requests received to root");
    // ... [Routing Logic] ...
}

The Issue:
Despite these calls, absolutely no log files are being generated under /Files/System/Log in our cloud environments.

Our Questions:

  1. Is "LogManager.Current" fully supported for file logging in DW10 Cloud? Or does this approach require additional manual configuration to write to /Files/System/Log?

  2. Should we be using standard ASP.NET Core Dependency Injection (ILogger<GeoRoutingMiddleware>) instead? If so, how do we configure the native ASP.NET Core logger to output to the /Files/System/Log folder so it can be viewed in the DW Insights UI?

Any guidance or documentation on the best practice for writing persistent custom logs in DW10 Cloud would be greatly appreciated.


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Ruwan,

LogManager.Current is fully supported in DW10 Cloud — it is not the API that's the problem. Here's what's most likely happening and how to fix it.

Most likely cause: log level threshold

The default global log threshold is Information. Debug calls are silently dropped unless you explicitly lower the threshold. Since you mentioned calling .Debug(...) in multiple places, those entries will never appear with the default config.

The threshold is read at startup from /Globalsettings/Settings/Logging/LogLevel in your GlobalSettings.xml. If that key is missing or not set to Debug, only Information, Warning, Error, and Fatal will be written to disk. Start by adding a .Warn(...) or .Error(...) call to confirm logging is working at all — if that appears in /Files/System/Log/MarketRouting/{date}.log, the pipeline is fine and you just need to lower the threshold.

To enable Debug logging, set this in Settings : https://doc.dynamicweb.dev/manual/dynamicweb10/settings/system/system/logRetention.html

GetLogger("MarketRouting") will write to:

/Files/System/Log/MarketRouting/{yyyy-MM-dd}.log
This is created automatically — no pre-existing directory needed.

Exceptions logged alongside an .Error(...) call also generate a detailed file at:

/Files/System/Log/MarketRouting/Errors/{yyyy}/{MM}/{MM}-{dd-HHmmss}.log
Regarding ILogger<T> (ASP.NET Core DI)

Do not switch to ILogger<GeoRoutingMiddleware> if your goal is visibility in DW Insights or /Files/System/Log. There is no built-in bridge that routes Microsoft's ILogger output to DW's file logging system — it goes to the console only. LogManager.Current.GetLogger(...) is the correct approach for DW-visible logs.

One caveat for middleware timing

If you're calling LogManager.Current very early in Program.cs (before WebApplication.Build() completes and DW initialises), SystemConfiguration.Instance may not be ready, which means ResetGlobalLogThreshold() falls back to Information and throws away your Debug calls anyway. Keep the logger calls inside the middleware class itself rather than in the Program.cs top-level code to be safe.

Summary checklist:

  • Confirm Error/Warn calls produce a file — this rules out a permissions or path issue
  • Set LogLevel to Debug in GlobalSettings.xml if you need debug output
  • Keep LogManager usage in the middleware class, not in Program.cs startup code
  • Do not use ILogger<T> — it bypasses DW's file logging entirely

 

You must be logged in to post in the forum