如何在c#中创建日志文件 [英] How to create log file in c#

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

问题描述

Hey Guys,

我正在使用桌面应用程序(基本上它是一个库存应用程序)。我想保持登录txt格式以了解应用程序崩溃的位置并维护其他事件。

请建议创建有意义的日志的最佳方法。

我真的很感激你的帮助。



我现在正在catch区创建txt文件。



谢谢!



我尝试了什么:



我试图创建日志文件c#。

Hey Guys,
I am working on a Desktop Application (basically it is a Inventory app). I would like to maintain log in txt format to know where application crashed and to maintain other events.
Please suggest best way to create log which is meaningful.
I really appreciate your help.

right now I am creating txt file in catch block.

Thanks!

What I have tried:

I have tried to create log file in c#.

推荐答案

You can find too many links on google that how to create a file and store in a location. And you can use that code in Catch block, so that if you get any exception it will log in that file.

Also you can find lot of things on google to handle error logs in Windows application.





如果你想要,你可以使用Elmah(第三方工具)将日志存储在数据库中。



现在是你选择你想要使用的选项。



干杯



If you want you can use Elmah(Third party tool) to store the logs in database.

Now it's you choice which option you want to use.

Cheers


请访问c#中创建日志文件的这些有用链接。 />


记录 - 如何在c#中写日志文件? - 堆栈溢出 [ ^ ]



http://www.dotnetpickles.com/2013/03/code-to-create-log-files-in-cnetaspnet.html [ ^ ]



如何用C#创建日志文件 [ ^ ]
Please visit these useful link about create log file in c#.

logging - How to write log file in c#? - Stack Overflow[^]

http://www.dotnetpickles.com/2013/03/code-to-create-log-files-in-cnetaspnet.html[^]

How to Create Log File in C#[^]


TRY ... CATCH是捕获异常的最佳实践(可能您可以使用exception.stackTrace获取更多详细信息日志),但有许多不同的方法来编写和维护日志。

1.应用程序启动路径中存储的.txt文件

2.存储登录数据库,(但是如果应用程序崩溃,很多时间数据库连接丢失,只会获得最后更新的消息而不是确切的代码行)

3.存储登录系统的应用程序事件日志



如果你寻找性能然后我可能会建议你'StringBuilder',因为它很快并且很容易附加所有文本

看下面的样本

TRY...CATCH is the best practice to catch the exception (may be you can use exception.stackTrace for more detail log), but there are many different ways to write and maintain log.
1. stored .txt file at application startup path
2. store log in database, (but if application crash in between many time database connection gets lost and will get only last updated message not exact code line)
3. Stored log in system's application event log

if you look for performance then I may suggest you 'StringBuilder', as it is fast and append all text easily
look at below sample
StringBuilder sb;
...
sb.Append("log something");

...
// flush every 20 seconds as you do it
File.AppendAllText(filePath+"log.txt", sb.ToString());
sb.Clear();



查看以下链接了解日志写作的更多细节

如何用C#编写日志文件 [ ^ ]


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

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