我在C#中遇到app.config
的问题。
我用app.config
和key
写了一个value=DEBUG
,而在main
我写了
DEBUG,INFO,WARN AND ERROR.
问题是该值不起作用,并且在不考虑级别的情况下将main
打印到我的日志中。
这是我的应用配置:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="FolderName" value="C:\Users\mpilas\Desktop\logs\" />
<add key="FileSize" venter code herealue="10240" />
<add key="LogNameType" value="Date"/> <!--Value can be Size or "Date" -->
<add key="Level" value='DEBUG'/>
</appSettings>
</configuration>
我的main
是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
namespace ConsoleApplication8 {
class Program {
static void Main(string[] args) {
for (int i = 0; i < 2; i++) {
LogWriter.Write("Hello", Severity.DEBUG);
LogWriter.Write("Hello", Severity.INFO);
LogWriter.Write("Hello", Severity.WARNING);
LogWriter.Write("Hello", Severity.ERROR);
}
}
}
}
我还有一个名为:LogWriter
的课程。
namespace ConsoleApplication8 {
public enum Severity { ERROR, WARNING, INFO, DEBUG };
public class LogWriter {
private static string _folder = ConfigurationManager.AppSettings["FolderName"];
private static long _fileSize = int32.Parse(ConfigurationManager.AppSettings["FileSize"]);
private static string kindOfType = ConfigurationManager.AppSettings["LogNameType"];
static LogWriter() {}
private static string GetFileNameByDate() {
string dateName= DateTime.Now.ToString("yyyy-MM-dd");
return _folder + dateName + ".log";
}
private static string GetFileNameBySize() {
string name = "";
int counter = 0;
do {
if (counter == 0) { name = _folder + kindOfType + ".log"; }
else {name = _folder + kindOfType + "-" + counter + ".log"; }
if (File.Exists(name) {
counter++;
FileInfo realFile = new FileInfo(name);
if (realFile.Length > _fileSize) {
name = _folder + kindOfType + "-" + counter + ".log";
}
else { break; }
}
else { break; }
} while (true);
return name;
}
private static string GetFileName() {
clearFiles();
if(kindOfType == "Size") { return GetFileNameBySize(); }
else {
if(kindOfType == "Date") { return GetFileNameByDate(); }
}
return "";
}
public static void Write(string messgae, Severity severity) {
try {
string file = GetFileName();
StreamWriter log = new StreamWriter(file, true);
log.WriteLine(DateTime.Now.ToString() + " " + severity.ToString() + ": [" + messgae + "]");
log.Flush();
log.Close();
}
catch (Exception) {}
}
private static void clearFiles() {
string[] files = Directory.GetFiles(_folder);
foreach (string file in files) {
FileInfo fi = new FileInfo(file);
if (fi.LastAccessTime < DateTime.Now.AddDays(-14)) { fi.Delete(); }
}
}
}
}
我应该如何处理key=level
及其在我的申请中的价值?
答案 0 :(得分:1)
您可能最好使用System.Diagnostics
中提供的现有日志脚手架 - 实现您自己的TraceListener
,它可以处理您的14天滚动日志等,然后您需要做的就是注册通过app.config
跟踪侦听器,然后使用trace switch配置跟踪级别。
请注意,支持代码中的traceSwitch
实例由<add name="TraceLevelSwitch" value="0" />
配置字段控制。
<configuration>
<system.diagnostics>
<switches>
<!-- 0-off, 1-error, 2-warn, 3-info, 4-verbose. -->
<add name="TraceLevelSwitch" value="0" />
</switches>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="myListener" type="MorPilasTraceListener" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
private static TraceSwitch traceSwitch = new TraceSwitch("TraceLevelSwitch", null);
private static void LogInfo(string message)
{
if(traceSwitch.TraceInfo)
Trace.TraceInformation(message);
}