Asp.NET MVC,自TextWriterTraceListener会不会创建一个文件 [英] Asp.NET MVC, custom TextWriterTraceListener does not create a file
问题描述
有关MVC应用程序的定制监听器不创建一个日志文件时使用initializeData =CustomWeblog.txt参数,但initializeData =D:\\ CustomWeblog.txt触发文件创建。什么是这种行为的原因是什么?控制台应用程序生成的文件为所有类型的听众。
For MVC application custom listener does not create a log file when initializeData="CustomWeblog.txt" parameter is used, but initializeData="d:\CustomWeblog.txt" triggers file creation. What is the reason of such behaviour? Console application generates files for all types of listeners.
自定义类:
public class CustomTextWriterTraceListener : TextWriterTraceListener
{
public CustomTextWriterTraceListener(string fileName) : base(fileName)
}
的Web.config(MVC应用程序的web.config)
Web.config (mvc application, 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>
对自定义监听器不会创建日志文件。
Cutom listener does not create a log file.
来电:
TraceSource obj = new TraceSource("Trace", SourceLevels.All);
obj.TraceEvent(TraceEventType.Critical,0,"This is a critical message");
我尝试添加一些额外的配置:从的这个博客和这个。但没有成功。我应该提供一个绝对路径?有任何解决方法通过定制监听器创建一个单独的程序?
I have tried to add some extra configuration: from this blog and this one. But there is no success. Should I provide a absolute path? Is there any workaround by creating a separate assembly for custom listener?
推荐答案
我试图创建自己的滚动文本编写跟踪侦听器,当我遇到您所描述的同样的问题。长话短说,所有运行在这里以后就是我想出了。
I was trying to create my own rolling text writer trace listener when I was encountering the same issue you described. Long story short, after all the running around here is what I came up with.
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
}
更新:下面是工具类,我写了解决的路径。
UPDATE: Here is the utility class I wrote for resolving paths.
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;
}
}
所以这个工具类可以让我来解决相对路径和还可以自定义格式。例如,如果你看了code,你会看到,应用程序的名称应用程序名
变量没有在这个路径中
"~/Logs/{ApplicationName}_{DateTime:yyyy-MM-dd}.log"
我能够配置应用程序的启动与任何其他变量我想补充像这样沿着
I am able to configure that in the startup of the application along with any other variables I want to add like so
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;
};
}
}
这篇关于Asp.NET MVC,自TextWriterTraceListener会不会创建一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!