标签:
1。日记记录主要是当程序出现了BUG时候记录到数据库时候出现了问题。记录不了。就记录到Windows日志里面 具体用法如下:
需要引入(NuGet)EnterpriseLibrary.Common
public void LogGeneral(GeneralLogEntry logEntry) {
var customLogFormatter = new GeneralLogFormatter("ld", "LogGeneral"); //使用代码设置
if (!EventLog.SourceExists("TCMSSix"))
{
EventLog.CreateEventSource("TCMSSix", "Application"); //MessageBox.Show("创建TCMSSix源");
}
EventLog eventLog = new EventLog("Application", ".", "TCMSSix"); //用TraceListener的派生类来包装
var formattedEventLogTraceListener = new FormattedEventLogTraceListener(eventLog, customLogFormatter);
var consoleTraceListener = new ConsoleTraceListener();
var config = new LoggingConfiguration(); //设置为异步用AddAsynchronousTraceListener
config.AddLogSource("Trace", SourceLevels.All, true).AddTraceListener(formattedEventLogTraceListener);
config.LogSources["Trace"].AddAsynchronousTraceListener(consoleTraceListener);
Logger.Reset();
Logger.SetLogWriter(new LogWriter(config));
Logger.Write(logEntry);
}
将错误转化成需要写入Windows日志的字符串
internal class GeneralLogFormatter : ILogFormatter
{
private readonly NameValueCollection _attributes;
public GeneralLogFormatter()
{
}
public GeneralLogFormatter(NameValueCollection attributes) { _attributes = attributes; }
public GeneralLogFormatter(string prefix, string ns) { _attributes = new NameValueCollection(); _attributes["prefix"] = prefix; _attributes["ns"] = ns; }
public string Format(LogEntry log) {
GeneralLogEntry logs = (GeneralLogEntry) log; //错误实体类
string prefix = _attributes["prefix"];
string ns = _attributes["ns"];
using (var sw = new StringWriter())
{
var w = new XmlTextWriter(sw);
w.Formatting = Formatting.Indented;
w.Indentation = 2;
w.WriteStartDocument(true);
w.WriteStartElement(prefix, "Log", ns);
w.WriteAttributeString("ServerKey", ns, 1.ToString());
w.WriteElementString("Processor", ns, logs.Processor);
w.WriteElementString("InstalledMemory", ns, logs.InstalledMemory.ToString());
w.WriteElementString("WindowsEdition", ns, logs.WindowsEdition);
w.WriteElementString("SystemType", ns, logs.SystemType);
w.WriteElementString("ComputerName", ns, logs.ComputerName);
w.WriteElementString("MVirgilUserPkey", ns, logs.MVirgilUserPkey.ToString());
.....//错误信息
w.WriteEndElement();
w.WriteEndDocument();
return sw.ToString();
}
}
}
当记录时候需要创建一下
if (!EventLog.SourceExists("TCMSSix"))
{
EventLog.CreateEventSource("TCMSSix", "Application"); //MessageBox.Show("创建TCMSSix源");
}
同时需要获取管理权限才能创建:
1.新建一个程序清单(.manifest)
2.
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC 清单选项 如果要更改 Windows 用户帐户控制级别,请用以下节点之一替换 requestedExecutionLevel 节点。
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
指定 requestedExecutionLevel 节点将会禁用文件和注册表虚拟化。
如果要利用文件和注册表虚拟化实现向后
兼容性,则删除 requestedExecutionLevel 节点。 -->
<!--<requestedExecutionLevel level="asInvoker" uiAccess="false" />-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
标签:
原文地址:http://www.cnblogs.com/wlwenjie/p/4440837.html