gpt4 book ai didi

c# - 企业库日志记录和文本格式化程序模板 token 。如何?

转载 作者:行者123 更新时间:2023-11-30 18:21:03 41 4
gpt4 key购买 nike

我正在尝试让我的日志记录组件按照我的意愿工作。当我看到这篇文章时 How to configure Enterpise Library 6.0 logging to database?

我发现格式化程序:

<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Application: {property(ApplicationName)}{newline}&#xA;Guid: {property(HandlingInstanceId)}{newline}&#xA;Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Exception type: {property(ExceptionType)}{newline}&#xA;Category: {category}{newline}&#xA;Severity: {severity}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Stack trace: {property(StackTrace)}{newline}&#xA;File: {property(FileName)}{newline}&#xA;Line: {property(LineNumber)}{newline}"
name="Text Formatter" />
</formatters>

包含我不知道如何使用的“Text Formatter Template Tokens”?

"Stack trace: {property(StackTrace)}{newline}&#xA"

目前我的日志记录类如下所示:

    public class Logging : Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry
{
/// <summary>
/// Logs an error message.
/// </summary>
/// <param name="Message"></param>
/// <param name="processEvent"></param>
public void LogToFile(string message, System.Diagnostics.TraceEventType severity)
{
try
{
LogWriterFactory logWriterFactory = new LogWriterFactory();
LogWriter logWriter = logWriterFactory.Create();
LogEntry logEntry = new LogEntry();
LogSource logsource = new LogSource("ESRAlarmStatistik");

logEntry.Message = message.ToString();
//logEntry.EventId = Convert.ToInt32(processEvent);
logEntry.Priority = 2;
logEntry.Severity = severity;
logEntry.TimeStamp = System.DateTime.Now;

logWriter.Write(logEntry);

//Unlocks the logfile so that it's possible to delete it.
logWriter.Dispose();
}
catch (LoggingException ex)
{
throw ex;
}
}
}

我想记录有关异常的更多信息,而不仅仅是消息。例如。文本格式化程序模板 token 建议的堆栈跟踪是可能的。

但是,我不知道该怎么做?有没有人对如何实现对上述文本格式化程序的支持有任何经验?

此时日志输出如下所示:

----------------------------------------
Application: <Error: property ApplicationName not found>

Guid: <Error: property HandlingInstanceId not found>

Timestamp: 2016-04-03 10:16:20

Message: IngresDatabaseAccessor.DBConnect() - Database connection failed. Connection string: Host=192.168.0.114;User Id=ingres;PWD=ingres;Database=esra_bas---Database does not exist: 'esrk_bas'.
The connection to the server has been aborted.

Exception type: <Error: property ExceptionType not found>

Category: General

Severity: Error

Machine: KSD53

App Domain: ESRAlarmStatistik.exe

ProcessId: 15464

Process Name: C:\DATA\Development\Uddeholm\ESR\Source\Common\ESRAlarmStatistik\ESRAlarmStatistik\bin\Debug\ESRAlarmStatistik.exe

Stack trace: <Error: property StackTrace not found>

File: <Error: property FileName not found>

Line: <Error: property LineNumber not found>

----------------------------------------

亲切的问候!

附言我正在使用 Enterprise Library 6.0

最佳答案

它看起来像你的

LogEntry logEntry = new LogEntry();

不包含格式化程序请求的属性:

logEntry.StackTrace = "missing Informaton in your LogToFile Method"

所以我认为,你必须扩展它。

关于c# - 企业库日志记录和文本格式化程序模板 token 。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36384328/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com