How can I output errors when using .less programmatically?

后端 未结 6 1160
广开言路
广开言路 2020-12-16 17:16

I\'ve written an ASP.NET MVC action method that receives a .less file name, processes it via Less.Parse() and outputs the processed css file.

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 17:41

    I just faced this today in my RequestReduce project. I was getting blank less -> css transforms because there were parse errors that appeared to be going into the ether. Thanks to qes's answer I was able to work out a solution where I could write the errors to the response stream. Here is my dotless.Core.Loggers.ILogger:

    public class LessLogger : ILogger
    {
        public void Log(LogLevel level, string message)
        {
        }
    
        public void Info(string message)
        {
        }
    
        public void Debug(string message)
        {
        }
    
        public void Warn(string message)
        {
        }
    
        public void Error(string message)
        {
            Response.Write(message);
        }
    
        public HttpResponseBase Response { get; set; }
    }
    

    I pass this into the Configuration sent to the EngineFactory:

                var engine = new EngineFactory(new DotlessConfiguration
                                                   {
                                                       CacheEnabled = false,
                                                       Logger = typeof (LessLogger)
                                                   }
                    ).GetEngine();
    

    For unit testing purposes I wanted to pass in my HttpResponseBase that would write the error. This is where I felt things getting ugly with some nasty casting to get a reference to my logger:

                ((LessLogger)((LessEngine)((ParameterDecorator)engine).Underlying).Logger).Response = response;
    

    I hope this helps out and if someone knows of a more elegant way to get a reference to the logger, please let me know.

提交回复
热议问题