C# 从控制台应用程序登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12890204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logging from a console application
提问by user1746174
I have created a console application which transfers data from an excel sheet and transfers it to a database. I require to do logging into it.
我创建了一个控制台应用程序,它从 Excel 工作表传输数据并将其传输到数据库。我需要登录它。
How can i go about it? because i have absolutely no idea about how to go about it.
我该怎么办?因为我完全不知道如何去做。
采纳答案by Chimera
If you simply want to write text to a file as your log you can do something like the following:
如果您只是想将文本作为日志写入文件,您可以执行以下操作:
string strLogText = "Some details you want to log.";
// Create a writer and open the file:
StreamWriter log;
if (!File.Exists("logfile.txt"))
{
log = new StreamWriter("logfile.txt");
}
else
{
log = File.AppendText("logfile.txt");
}
// Write to the file:
log.WriteLine(DateTime.Now);
log.WriteLine(strLogText);
log.WriteLine();
// Close the stream:
log.Close();
Source of this code is from this article.
Here is some documentation of the StreamWriter Class.
此代码的来源来自本文。
这是StreamWriter 类的一些文档。
Otherwise if you want something more you can do as others have suggested and check out Log4Net.
否则,如果您想要更多的东西,您可以按照其他人的建议进行操作并查看Log4Net。
回答by Peru
there are many ways to log. You can use your own logging
登录的方式有很多种。您可以使用自己的日志记录
else try Log4net it is simple configuration changes you are done.
否则试试 Log4net 简单的配置更改就完成了。
Good article http://www.codeproject.com/Articles/140911/log4net-Tutorial
好文章http://www.codeproject.com/Articles/140911/log4net-Tutorial
Just an additional info for you reg ELMAH(Another way of error logging) Using ELMAH in a console application
只是一个额外的信息 reg ELMAH(错误记录的另一种方式) 在控制台应用程序中使用 ELMAH
回答by Saurabh R S
回答by inquam
I require to do logging into it....
I require to do logging into it....
Do you mean you wish to add logging to the application or that you want people to log in?
您的意思是您希望向应用程序添加日志记录还是希望人们这样做log in?
To log thing to a file Log4Netexists to make it easy for you. If you don't want to rely on third party stuff then you could just do something like
将事物记录到文件Log4Net 的存在是为了方便您。如果你不想依赖第三方的东西,那么你可以做一些类似的事情
Trace.Listeners.Clear();
if (options.Verbose)
{
var ctl = new ConsoleTraceListener(false) { TraceOutputOptions = TraceOptions.DateTime };
Trace.Listeners.Add(ctl);
}
if (options.Log)
{
var logFileFs = new FileStream("my.log", FileMode.Append);
var twtl = new TextWriterTraceListener(logFileFs)
{ TraceOutputOptions = TraceOptions.ThreadId | TraceOptions.DateTime };
Trace.Listeners.Add(twtl);
}
Trace.AutoFlush = true;
Trace.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + " My App Started");
This enables you to add different outputs such as a log file and verbose printing to your ouput so that you could log to a file and also print it in console if the user has choose to use verbose output.
这使您可以向输出添加不同的输出,例如日志文件和详细打印,以便您可以登录到文件并在用户选择使用详细输出时在控制台中打印它。
The line
线
Trace.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + " My App Started");
should be made into a function that takes your message and perhaps a log level so you have a uniform look to all your written log messages.
应该被做成一个函数,它接收你的消息,也许是一个日志级别,这样你就可以统一查看所有写入的日志消息。
If you don't need that much functionality you could just write to the file stream directly.
如果您不需要那么多功能,您可以直接写入文件流。

