c# - Asp.NET MVC,自定义TextWriterTraceListener不创建文件

标签 c# .net asp.net-mvc configuration tracesource

对于 MVC 应用程序,自定义监听器在使用initializeData="CustomWeblog.txt"参数时不会创建日志文件,但initializeData="d:\CustomWeblog.txt"会触发文件创建。这种行为的原因是什么?控制台应用程序为所有类型的监听器生成文件。

自定义类:

public class CustomTextWriterTraceListener : TextWriterTraceListener 
{ 
     public CustomTextWriterTraceListener(string fileName) : base(fileName)
}

Web.config(mvc 应用程序,web.config)

<trace autoflush="true" />
<sources>
  <source name="Trace">    
      <listeners>
        <add name="TextWriterListner"
             type="System.Diagnostics.TextWriterTraceListener, WebTracing" initializeData="Weblog.txt"/>
        <!-- the file is created -->
        <add name="CustomTextWriterListner"
             type="WebTracing.CustomTextWriterTraceListener, WebTracing" initializeData="CustomWeblog.txt"/>
        <!-- the file is not created in MVC application ?! -->
        <add name="CustomTextWriterListnerAbsolutePath"
             type="WebTracing.CustomTextWriterTraceListener, WebTracing" initializeData="d:\CustomWeblog.txt"/>
        <!-- the file is created -->
      </listeners>      
  </source>
</sources>

自定义监听器不创建日志文件。

来电者:

TraceSource obj = new TraceSource("Trace", SourceLevels.All);
obj.TraceEvent(TraceEventType.Critical,0,"This is a critical message");

我尝试添加一些额外的配置:来自 this blogthis one 。但没有成功。我应该提供绝对路径吗?通过为自定义监听器创建单独的程序集是否有任何解决方法?

最佳答案

当我遇到您描述的相同问题时,我正在尝试创建自己的滚动文本编写器跟踪监听器。长话短说,毕竟这里是我跑来跑去才想出来的。

public class RollingTextWriterTraceListener : TextWriterTraceListener {
    string fileName;
    private static string[] _supportedAttributes = new string[] 
        { 
            "template", "Template", 
            "convertWriteToEvent", "ConvertWriteToEvent",
            "addtoarchive","addToArchive","AddToArchive",
        };

    public RollingTextWriterTraceListener(string fileName)
        : base() {
        this.fileName = fileName;
    }
    /// <summary>
    /// This makes sure that the writer exists to be written to.
    /// </summary>
    private void ensureWriter() {
        //Resolve file name given. relative paths (if present) are resolved to full paths.
        // Also allows for paths like this: initializeData="~/Logs/{ApplicationName}_{DateTime:yyyy-MM-dd}.log"
        var logFileFullPath = ServerPathUtility.ResolvePhysicalPath(fileName);
        var writer = base.Writer;
        if (writer == null && createWriter(logFileFullPath)) {
            writer = base.Writer;
        }
        if (!File.Exists(logFileFullPath)) {
            if (writer != null) {
                try {
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                } catch (ObjectDisposedException) { }
            }
            createWriter(logFileFullPath);
        }
        //Custom code to package the previous log file(s) into a zip file.
        if (AddToArchive) {
            TextFileArchiveHelper.Archive(logFileFullPath);
        }
    }

    bool createWriter(string logFileFullPath) {
        try {
            logFileFullPath = ServerPathUtility.ResolveOrCreatePath(logFileFullPath);
            var writer = new StreamWriter(logFileFullPath, true);
            base.Writer = writer;
            return true;
        } catch (IOException) {
            //locked as already in use
            return false;
        } catch (UnauthorizedAccessException) {
            //ERROR_ACCESS_DENIED, mostly ACL issues
            return false;
        }
    }

    /// <summary>
    /// Get the add to archive flag
    /// </summary>
    public bool AddToArchive {
        get {
            // Default behaviour is not to add to archive.
            var addToArchive = false;
            var key = Attributes.Keys.Cast<string>().
                FirstOrDefault(s => string.Equals(s, "addtoarchive", StringComparison.InvariantCultureIgnoreCase));
            if (!string.IsNullOrWhiteSpace(key)) {
                bool.TryParse(Attributes[key], out addToArchive);
            }
            return addToArchive;
        }
    }

    #region Overrides
    /// <summary>
    /// Allowed attributes for this trace listener.
    /// </summary>
    protected override string[] GetSupportedAttributes() {
        return _supportedAttributes;
    }

    public override void Flush() {
        ensureWriter();
        base.Flush();
    }

    public override void Write(string message) {
        ensureWriter();
        base.Write(message);
    }

    public override void WriteLine(string message) {
        ensureWriter();
        base.WriteLine(message);
    }
    #endregion
}

更新:这是我为解析路径编写的实用程序类。

public static class ServerPathUtility {

    public static string ResolveOrCreatePath(string pathToReplace) {
        string rootedFileName = ResolvePhysicalPath(pathToReplace);
        FileInfo fi = new FileInfo(rootedFileName);
        try {
            DirectoryInfo di = new DirectoryInfo(fi.DirectoryName);
            if (!di.Exists) {
                di.Create();
            }
            if (!fi.Exists) {
                fi.CreateText().Close();
            }
        } catch {
            // NO-OP
            // TODO: Review what should be done here.
        }
        return fi.FullName;
    }

    public static string ResolvePhysicalPath(string pathToReplace) {
        string rootedPath = ResolveFormat(pathToReplace);
        if (rootedPath.StartsWith("~") || rootedPath.StartsWith("/")) {
            rootedPath = System.Web.Hosting.HostingEnvironment.MapPath(rootedPath);
        } else if (!Path.IsPathRooted(rootedPath)) {
            rootedPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, rootedPath);
        }
        return rootedPath;
    }

    public static string ResolveFormat(string format) {
        string result = format;

        try {
            result = ExpandApplicationVariables(format);
        } catch (System.Security.SecurityException) {
            // Log?
        }

        try {
            string variables = Environment.ExpandEnvironmentVariables(result);
            // If an Environment Variable is not found then remove any invalid tokens
            Regex filter = new Regex("%(.*?)%", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

            string filePath = filter.Replace(variables, "");

            if (Path.GetDirectoryName(filePath) == null) {
                filePath = Path.GetFileName(filePath);
            }
            result = filePath;
        } catch (System.Security.SecurityException) {
            // Log?
        }

        return result;
    }

    public static string ExpandApplicationVariables(string input) {
        var filter = new Regex("{(.*?)}", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
        var result = filter.Replace(input, evaluateMatch());
        return result;
    }

    private static MatchEvaluator evaluateMatch() {
        return match => {
            var variableName = match.Value;
            var value = GetApplicationVariable(variableName);
            return value;
        };
    }

    public static string GetApplicationVariable(string variable) {
        string value = string.Empty;
        variable = variable.Replace("{", "").Replace("}", "");
        var parts = variable.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
        variable = parts[0];
        var parameter = string.Empty;
        if (parts.Length > 1) {
            parameter = string.Join("", parts.Skip(1));
        }

        Func<string, string> resolve = null;
        value = VariableResolutionStrategies.TryGetValue(variable.ToUpperInvariant(), out resolve) && resolve != null
            ? resolve(parameter)
            : string.Empty;

        return value;
    }

    public static readonly IDictionary<string, Func<string, string>> VariableResolutionStrategies =
        new Dictionary<string, Func<string, string>> {
            {"MACHINENAME", p => Environment.MachineName },
            {"APPDOMAIN", p => AppDomain.CurrentDomain.FriendlyName },
            {"DATETIME", getDate},
            {"DATE", getDate},
            {"UTCDATETIME", getUtcDate},
            {"UTCDATE", getUtcDate},
        };

    static string getDate(string format = "yyyy-MM-dd") {
        var value = string.Empty;
        if (string.IsNullOrWhiteSpace(format))
            format = "yyyy-MM-dd";
        value = DateTime.Now.ToString(format);
        return value;
    }

    static string getUtcDate(string format = "yyyy-MM-dd") {
        var value = string.Empty;
        if (string.IsNullOrWhiteSpace(format))
            format = "yyyy-MM-dd";
        value = DateTime.Now.ToString(format);
        return value;
    }
}

所以这个实用程序类允许我解析相对路径并自定义格式。例如,如果您查看代码,您会发现该路径中不存在应用程序名称 ApplicationName 变量

"~/Logs/{ApplicationName}_{DateTime:yyyy-MM-dd}.log"

我可以在应用程序启动时配置它以及我想添加的任何其他变量

public partial class Startup {
    public void Configuration(IAppBuilder app) {
        //... Code removed for brevity           
        // Add APPLICATIONNAME name to path Utility
        ServerPathUtility.VariableResolutionStrategies["APPLICATIONNAME"] = p => {
            var assembly = System.Reflection.Assembly.GetExecutingAssembly();
            if (assembly != null)
                return assembly.GetName().Name;
            return string.Empty;
        };           
    }
}

关于c# - Asp.NET MVC,自定义TextWriterTraceListener不创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35821407/

相关文章:

c# - 操作 .NET System.Console 窗口大小会导致 ArgumentOutOfBoundsException

c# - 在 C#/.NET 环境中对输入进行全局验证?

c# - 大对象堆浪费

sql - 如何在内容类型中最好地集成现有数据库数据

c# - 如何将参数和上传图片同时传递给 One web api?

c# - 如何动态创建 SSRS 报告?

c# - 如何在 DataGridView 控件中实现过滤?

html - MVC 中未加载 CSS 文件

c# - 未找到匹配项时从 List<> 返回默认值

c# - 对象.GetHashCode