如何从单独的项目中编写公共文件? [英] How Do I Write In A Common File From Separate Projects?

查看:74
本文介绍了如何从单独的项目中编写公共文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何从单独的项目中编写公共文件?

我在初始化每个项目时创建TextWriter,然后在整个项目中写入日志。

Hi,
How do i write in a common file from separate projects?
i create TextWriter in Initializing each project, then write logs in whole of projects.

_Writer = TextWriter.Synchronized(new StreamWriter(path));





当需要写日志时,我打电话:



when it needs to write log, i call:

_Writer.WriteLine(line);
     _Writer.Flush()





但是当第一个项目运行时,第二个项目无法创建TextWriter(第二个项目) )不会在常见的日志文件中写任何东西。



如何在共享写入模式下为公共路径中的文件创建TextWriter对象?



but when first project runs, second one cann't create TextWriter then it(second project) doesn't write any thing in common log file.

How do i create TextWriter object in share writing mode for file in common path ?

推荐答案

不要保持文件打开 - 特别是日志文件,但通常不管怎么说都要打开它们。

而不是,像这样做你的日志:

Don't hold files open - particularly log files, but generally it's a bad idea to hold them open anyway.
Instead, do your logging like this:
File.AppendAllText(logFilePath, "String to log\n");



见这里: http://msdn.microsoft.com/en-us/library/ms143356(v = vs.110)的.aspx [< a href =http://msdn.microsoft.com/en-us/library/ms143356(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]



您可能想把它放在ar中如果你有多个应用程序写入同一个日志文件,那么etry循环和try ... catch块只是为了安全起见。


See here: http://msdn.microsoft.com/en-us/library/ms143356(v=vs.110).aspx[^]

You may want to put this in a retry loop and a try...catch block just to be on the safe side if you have multiple apps writing to the same log file.


这不是一个合适或可靠的写作方式日志。相反,您可以使用类 System.Diagnostics.EventLog

http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog%28v=vs.110% 29.aspx [ ^ ]。



另一种选择是使用Apache log4net: http://logging.apache.org/log4net [ ^ ]。



-SA
This is not an appropriate or reliable way for writing logs. Instead, you can use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog%28v=vs.110%29.aspx[^].

Another option is using Apache log4net: http://logging.apache.org/log4net[^].

—SA


Zon-cpp通过EventLog询问:
Zon-cpp asked:



,我可以在exe文件旁指定一条特殊路径吗?


by EventLog, can i specify a special path beside the exe file?

谁告诉你 path是exe文件。这不是它的工作原理。



您可以自定义 EventLog 来更改数据接收器,使其成为可能一个文件或其他任何东西,但我建议使用默认行为。要了解具体方法,请参阅我之前的回答:在Windows应用程序中动态地将MsBuild输出到TextBox [ ^ ]。



即使使用默认接收器,系统事件日志,要由标准事件日志查看器查看,您可以注册(以及以后取消注册)自定义事件源,以将事件存储在事件树的单独的特定于应用程序的节点中。请参阅我之前的回答:如何创建事件日志一个文件夹 [ ^ ]。



-SA

Who told you that the "path" is "exe file". This is not how it works.

You can customize EventLog to change the sink of data, to make it a file or anything else, but I would recommend to use default behavior. To understand how, please see my past answer: MsBuild OutPut to the TextBox on the fly in Windows Application[^].

Even if you use default sink, system event log, to be viewed by the standard Event Log viewer, you can register (and later unregister) your custom event source, to have your events stored in a separate application-specific node of the event tree. Please see my past answer: How to create event log under a folder[^].

—SA


这篇关于如何从单独的项目中编写公共文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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