如何在Project中引发错误日志文件 [英] How to Create a log file For Errors raises in Project

查看:92
本文介绍了如何在Project中引发错误日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在使用Windows应用程序.

在我的项目中,我必须为运行该项目时引发的错误创建一个lof文件.
点网框架的跟踪和调试类在这里有用吗?
还有其他方法吗?

Hello,

I am working on a windows application.

In my project I have to create a lof file for the errors which are raises while running the project.
Are the trace and debug classes of dot net framework are useful here?
Is there any other way to do this?

推荐答案

确实,这取决于您如何执行.有时候,一个简单的文本文件就足够了,而有时候数据库表是有用的.

如果我已经在使用数据库,则倾向于登录数据库表(或实际上是一个单独的数据库),因为它在多用户环境中既简单又可靠.

如果没有,那么文本文件就可以了!这里有一篇有关文本文件日志记录的文章:使用ASP.NET和C#创建简单错误日志文件 [ ^ ]

还是在这里有我的标准数据库日志记录:
Really and truly, it is up to you how you do it. Sometimes, a simple text file is sufficient, other times a database table is useful.

If I am already using a database, I tend to log to a database table (or a separate database in fact) as it is simple and reliable in a multi user environment.

If not, then a text file will do fine! There is an article on Text file logging here: Create Simple Error Log Files using ASP.NET and C#[^]

Or there is my standard DB logging here:
/// <summary>
/// Create a log entry in the database
/// </summary>
public LogEntry(string message, ErrorLogCause cause, DateTime when, object additionalInfo)
    {
    string excess = null;
    while (message.Length > 2048)
        {
        // Too big - truncate and add ellipsis
        excess = message.Substring(2048 - 3);
        message = message.Substring(0, 2048 - 3) + "...";
        if (additionalInfo == null)
            {
            // Great! We can save it anyway!
            additionalInfo = excess;
            excess = null;
            }
        }
    byte[] add = additionalInfo as byte[];
    AdditionalInfoType type = AdditionalInfoType.None;
    if (additionalInfo != null)
        {
        if (additionalInfo is string)
            {
            add = System.Text.Encoding.ASCII.GetBytes(additionalInfo as string);
            type = AdditionalInfoType.String;
            }
        else if (additionalInfo is byte[])
            {
            type = AdditionalInfoType.ByteArray;
            }
        else
            {
            new LogEntry("Unknown additionalInfo type supplied to LogEntry: " + additionalInfo.GetType().ToString(),
                         ErrorLogCause.ErrorInErrorReporting,
                         DateTime.Now,
                         message);
            type = AdditionalInfoType.Unknown;
            }
        }
    // Log the problem.
    string dbCon = ConfigurationManager.ConnectionStrings[dbConName].ConnectionString;
    try
        {
        using (SqlConnection con = new SqlConnection(dbCon))
            {
            con.Open();
            using (SqlCommand com = new SqlCommand("INSERT INTO SMAdmin.Log (message, loggedAt, cause, infoType, additionalInfo) " +
                                                   "VALUES (@M, @A, @C, @T, CAST(@I AS VARBINARY(MAX)))", con))
                {
                com.Parameters.AddWithValue("@M", message);
                com.Parameters.AddWithValue("@A", when);
                com.Parameters.AddWithValue("@C", cause);
                com.Parameters.AddWithValue("@T", type);
                if (add != null)
                    {
                    com.Parameters.AddWithValue("@I", add);
                    }
                else
                    {
                    com.Parameters.AddWithValue("@I", DBNull.Value);
                    }
                com.ExecuteNonQuery();
                }
            }
        }
    catch
        {
        // Anonymous catch: Error in error reporting system
        // Any attempt to record the problem will most likely makes things worse
        // as the whole system is to c**k anyway.
        }
    // Handle any extra info we couldn't fit in...
    if (excess != null)
        {
        // Problem: Issue as continuation error log and hope we can tie them together...
        new LogEntry("...Continuation...", ErrorLogCause.Continuation, DateTime.Now, excess);
        }
    }


请参阅我过去关于记录的两个答案:

将MsBuild OutPut即时发送到TextBox Windows应用程序中 [ ^ ]

如何在文件夹下创建事件日志 [ ^ ]

一个答案说明如何制作简单的自定义记录器(您可以使用标准的EventLog登录自定义接收器,例如文件或UI控件,另一个答案说明如何使用自定义结构登录系统日志(如何创建单独的记录器).应用程序的树节点(文件夹).

请注意,标准系统日志是最可靠的,因此,您最好选择它以确保在紧急情况下仍然有日志.

—SA
Please see two of my past Answers on logging:

MsBuild OutPut to the TextBox on the fly in Windows Application[^]

How to create event log under a folder[^]

One Answers shows how to make a simple custom logger (you can use the standard EventLog to log in the custom sink, such as file or UI control, another one shows how to log in the System Log with custom structure (how to create a separate tree node (folder) for your applications).

Please be advised that the standard System Log is most reliable, so you should prefer it to make sure you still have a log in critical situations.

—SA


我只是要提到我选择的武器- ^ ]

关于log4net的最有用的部分是使用相同的代码库,您只需添加一些配置条目即可驱动多个追加程序

例如

I''m just going to mention my weapon of choice - log4net[^]

The most useful part about log4net is with the same codebase, you can drive multiple appenders by simply adding a few configuration entries

For examples

if (log.IsInfoEnabled)
{
    log.Info("Some information message to write to the log file");
}



之后,您可以在配置文件中指定附加程序.

例如附加到SQL Server",附加到文本文件",附加到UDP端口"

Appender示例[



After that, you can specifiy appenders in your config file.

e.g ''Append to SQL Server'', ''Append to text file'', ''Append to UDP port''

Appender Examples[^]

So that line of code will append to as many targets as you configure. With absolutely no change to your code, you can append log information to various locations

I find log4net quite simple to work with & the options are really useful...I''m using it in some large production systems with no problems


这篇关于如何在Project中引发错误日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆