本文介绍了我输出的错误是如何在使用.LESS编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了收到.LESS文件名,通过 Less.Parse处理它的ASP.NET MVC的操作方法(小于名>)并输出处理CSS文件。

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

这只要.LESS code是有效的工作正常,但如果出现错误,带点只返回一个空字符串。所以,如果有一个错误处理文件,我的行动方法返回一个空的CSS文件。

This works fine as long as the .less code is valid, but if there is an error, dotLess just returns an empty string. So if there is an error processing the file, my action method returns an empty css file.

我如何输出和语法错误,而不是仔细描述的错误信息?

How can I output an error message with a closer description of the syntax error instead?

推荐答案

带点分析器捕获异常并把它们输出到记录器。从不带点的来源,执行该代码段为 LessEngine.TransformToCss

The dotLess parser traps Exceptions and outputs them to a Logger. The snippet from dotLess's source that performs this is LessEngine.TransformToCss:

public string TransformToCss(string source, string fileName)
{
    try
    {
        Ruleset ruleset = this.Parser.Parse(source, fileName);
        Env env = new Env();
        env.Compress = this.Compress;
        Env env2 = env;
        return ruleset.ToCSS(env2);
    }
    catch (ParserException exception)
    {
        this.Logger.Error(exception.Message);
    }
    return "";
}

Less.Parse 有需要 DotlessConfiguration 对象,它提供了几个属性,你可以用一个重载:

Less.Parse has an overload that takes a DotlessConfiguration object, which provides several properties that you can use:

public class DotlessConfiguration
{
    // Properties
    public bool CacheEnabled { get; set; }
    public Type LessSource { get; set; }
    public Type Logger { get; set; }
    public LogLevel LogLevel { get; set; }
    public bool MinifyOutput { get; set; }
    public int Optimization { get; set; }
    public bool Web { get; set; }
}

您会注意到日志属性的类型为键入的。无论你提供必须实现键入 dotless.Core.Loggers.ILogger

You will notice that the Logger property is of type Type. Whatever type you supply must implement dotless.Core.Loggers.ILogger:

public interface ILogger
{
    // Methods
    void Debug(string message);
    void Error(string message);
    void Info(string message);
    void Log(LogLevel level, string message);
    void Warn(string message);
}

正如我们在第一个片段中看到的,当在解析过程中遇到错误的记录器错误方法将被调用。

现在,这一切一个棘手的问题是究竟如何实现 ILogger 被实例化的类型的实例。在内部,不带点将使用烤成DLL IoC容器。继方法调用,看来,它最终将调用 Activator.CreateInstance 来实例化ILogger。

Now, the one sticky point of all this is how exactly an instance of the type that implements ILogger gets instantiated. Internally, dotLess uses an IoC container that is baked into the DLL. Following the method calls, it appears that it will eventually call Activator.CreateInstance to instantiate your ILogger.

我希望这至少是有点帮助的。

I hope this is at least somewhat helpful.

这篇关于我输出的错误是如何在使用.LESS编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 23:15