Taritsyn/WebMarkupMin

WebMarkupMin problem with big files and logging

Closed this issue · 4 comments

// logs
public static void Main(string[] args)
{
    Host.CreateDefaultBuilder(args)
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder
                .UseStartup<Startup>()
                .UseIISIntegration()
                .UseSerilog((context, configuration) =>
                {
                    configuration
                        .MinimumLevel.Debug()
                        .MinimumLevel.Override("Microsoft", LogEventLevel.Debug)
                        .MinimumLevel.Override("System", LogEventLevel.Debug)
                        .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Debug)
                        .Enrich.FromLogContext()
                        .WriteTo.File(@"Logs.txt");
                });
        })
        .Build()
        .Run();
}

Adding Services Code Block:

// adding services
services.AddSingleton<WebMarkupMin.Core.Loggers.ILogger, WebMarkupMin.Core.Loggers.ThrowExceptionLogger>();

services.AddWebMarkupMin(options =>
{
    options.MaxResponseSize = -1;
    options.AllowMinificationInDevelopmentEnvironment = true;
    options.AllowCompressionInDevelopmentEnvironment = true;
})
.AddHtmlMinification(options =>
{
    options.MinificationSettings.RemoveRedundantAttributes = true;
    options.MinificationSettings.RemoveHttpProtocolFromAttributes = true;
    options.MinificationSettings.RemoveHttpsProtocolFromAttributes = true;
})
.AddXmlMinification()
.AddHttpCompression();
// Middlewares
...
app.UseStaticFiles();
app.UseWebMarkupMin();
...

Hello, Akaki!

Swap the services.AddSingleton<WebMarkupMin.Core.Loggers.ILogger, …>() and services.AddWebMarkupMin(…) calls and logging will work:

// adding services
services.AddWebMarkupMin(options =>
{
    options.MaxResponseSize = -1;
    options.AllowMinificationInDevelopmentEnvironment = true;
    options.AllowCompressionInDevelopmentEnvironment = true;
})
.AddHtmlMinification(options =>
{
    options.MinificationSettings.RemoveRedundantAttributes = true;
    options.MinificationSettings.RemoveHttpProtocolFromAttributes = true;
    options.MinificationSettings.RemoveHttpsProtocolFromAttributes = true;
})
.AddXmlMinification()
.AddHttpCompression();

// override the default logger for WebMarkupMin.
services.AddSingleton<WebMarkupMin.Core.Loggers.ILogger, WebMarkupMin.Core.Loggers.ThrowExceptionLogger>();

As an example, you can look at the WebMarkupMin.Sample.AspNetCore8.Mvc8 project.

I also recommend replacing the ThrowExceptionLogger with your own implementation.

Does order of registering services have any difference?

Does order of registering services have any difference?

Yes, there is a difference! When calling the AddWebMarkupMin method, the following code is executed:

services.AddSingleton<ILogger, NullLogger>();
services.AddSingleton<ICssMinifierFactory, KristensenCssMinifierFactory>();
services.AddSingleton<IJsMinifierFactory, CrockfordJsMinifierFactory>();

In your case, NullLogger was still being used.

Hello, Akaki!

Version 2.18.0 introduces two new features that can make it easier to solve your problem:

  1. Services required by WebMarkupMin are now conditionally registered using the TryAdd methods.
  2. Added a AspNetCoreLogger class, which is a wrapper around the standard ASP.NET Core logger.

As an example, you can see the “ASP.NET Core Latest” section of documentation.