Taritsyn / WebMarkupMin

The Web Markup Minifier (abbreviated WebMarkupMin) - a .NET library that contains a set of markup minifiers. The objective of this project is to improve the performance of web applications by reducing the size of HTML, XHTML and XML code.
Apache License 2.0
440 stars 48 forks source link

More a question, not an "issue" #149

Closed twoelfer closed 1 year ago

twoelfer commented 1 year ago

I have a feeling this is really something totally trivial, but i can't seem to figure this out on my own. I am using webmarkupmin in my asp.net core app. I enable it like so:

(...) var builder = WebApplication.CreateBuilder(args);

        // Add services to the container.
        builder.Services.AddRazorPages();
        builder.Services.AddWebMarkupMin(options =>
        {
            //options.AllowMinificationInDevelopmentEnvironment = true;
            options.DisablePoweredByHttpHeaders = true;
        }).AddHtmlMinification();

and later:

        app.UseWebMarkupMin();
        app.UseRouting();

Now, i'm also using ApplicationInsights. And it works, i can use Application Insights to log from my own controllers and pages.

However, i simply cannot get WebMarkupMin to do logging.

(i have some pages that do not get minified. i assume, with logging turned on, i would get some information why this is the case. some years ago, my site was using asp.net and i used webmarkup min the "request" handler. one could simply look if the minification had errors and add those to the produced html output... i'm looking for something like that, in order to find out, why these "special" pages are not getting minified.)

Taritsyn commented 1 year ago

Hello, Thomas!

You can write your own logger or use the built-in logger (ThrowExceptionLogger class).

In your case, the logger for WebMarkupMin needs to be registered in the Program.cs file as follows:

…
using IWmmLogger = WebMarkupMin.Core.Loggers.ILogger;
using WmmThrowExceptionLogger = WebMarkupMin.Core.Loggers.ThrowExceptionLogger;
…

var builder = WebApplication.CreateBuilder(args);
…

// Override the default logger for WebMarkupMin.
builder.Services.AddSingleton<IWmmLogger, WmmThrowExceptionLogger>();
…
twoelfer commented 1 year ago

hi Taritsyn.

Thank you, this worked and i was a able to add the logger to WebMarkupMin. However, unfortunately, this did not help me with my problem as had hoped.

I still have pages that are not being minimized. (This is such a page: https://www.die.de/video/so-zeigen-sie-mehrere-ergebnisse-gleichzeitig-an/289 ) - (all of my pages with videos seem to have the same problem, so i guess it has something to do with that specific razor page).

However, i don't see any logging info concerning this. (I assume an exception should have been thrown due to a problem and this should have turned up in my application insights telemetry?

Do you have any other suggestion of how i could find out what is causing this problem?

twoelfer commented 1 year ago

hi Taritsyn.

I managed to find the problem; it was caused by some invalid html in the head of the page. I fixed the html and the minimization now works. However, i would still very much like to get some kind of error logging output from WebMarkupMin and i still don't get any... (So my operational problem is solved, but strategically, i still don't know what i am doing wrong...)

Taritsyn commented 1 year ago

However, i don't see any logging info concerning this. (I assume an exception should have been thrown due to a problem and this should have turned up in my application insights telemetry?

ThrowExceptionLogger just throws an MarkupMinificationException exception on error. If you need logging, then write your own class, that implements the ILogger interface or inherits the LoggerBase class from the WebMarkupMin.Core.Loggers namespace. And then register this class instead of the ThrowExceptionLogger class.

twoelfer commented 1 year ago

hm, thank you, but still no luck.

i implemented it like this:

public class MinimizerLogger : WebMarkupMin.Core.Loggers.LoggerBase { private readonly ILogger _logger;

public MinimizerLogger(ILogger<MinimizerLogger> logger)
{
    _logger = logger;
}

public override void Error(string category, string message, string filePath = "", int lineNumber = 0, int columnNumber = 0,
    string sourceFragment = "")
{
    base.Error(category, message, filePath, lineNumber, columnNumber, sourceFragment);
    _logger.LogError(message, new object?[]{ category, filePath, lineNumber, columnNumber, sourceFragment });
    //base.Error(category, message, filePath, lineNumber, columnNumber, sourceFragment);
}

public override void Debug(string category, string message, string filePath = "")
{
    base.Debug(category, message, filePath);
    _logger.LogError(message);
}

public override void Warn(string category, string message, string filePath = "", int lineNumber = 0, int columnNumber = 0,
    string sourceFragment = "")
{
    base.Debug(category, message, filePath);
    _logger.LogError(message);
}

}

and i register it like this:

builder.Services.AddSingleton<IWmmLogger, MinimizerLogger>();

However, still no logging. As a matter of fact, the MinimizerLogger constructor isn't even called.

Taritsyn commented 1 year ago

base.Error(category, message, filePath, lineNumber, columnNumber, sourceFragment);

Calls of base methods are unnecessary because it are empty.

In fact, such a logger should work. I implemented similar functionality in a demo web application and everything works for me:

wmm-logging

Only problem is that in this case the location of error was calculated incorrectly, but this is another error.

twoelfer commented 1 year ago

Thank you very much, your demo web application helped a lot. I got it to work. Turns out, the problem was the order of the Add() calls. Added WebMarkupMin after the AddSingleton() call. Changed the order of the calls and now it works. Thank you!

twoelfer commented 1 year ago

Thanks again,

Taritsyn commented 1 year ago

I'm glad I could help.