[英]How to assign C# output message to a string variable?
Like, being able to store 喜欢,能够存储
string val = Console.WriteLine("Hello")
or equivalent way. 或等效方式。 Type casting doesn't seem to work neither.
类型转换似乎也不起作用。
Update:: Well, I want to log whatever Console.WriteLine() outputs, so question is what is the best way to accomplish that? 更新::好吧,我想记录任何Console.WriteLine()输出,所以问题是什么是实现这一目标的最佳方法?
var s = "Hello";
Console.WriteLine(s);
or 要么
Func<string,string> ConsoleWriteLine = input => {Console.WriteLine(input); return input;};
and then 接着
var x = ConsoleWriteLine("Hello"); // x == "Hello" now.
Let me ask you though: why? 让我问你:为什么? What are you trying to accomplish?
你想达到什么目的? We might help you with that instead ;)
我们可能会帮助你;)
EDIT: asker said he wants the Console.WriteLine to write to the file (I'm guessing: as well). 编辑:asker说他希望Console.WriteLine写入文件(我猜:以及)。
A way to accomplish that might be to have a custom TextWriter set to System.Console.Out, as described here: https://msdn.microsoft.com/en-us/library/system.console.out%28v=vs.110%29.aspx 实现这一目标的方法可能是将自定义TextWriter设置为System.Console.Out,如下所述: https : //msdn.microsoft.com/en-us/library/system.console.out%28v=vs。 110%29.aspx
The actual setting is using Console.SetOut ( https://msdn.microsoft.com/en-us/library/system.console.setout%28v=vs.110%29.aspx ). 实际设置使用Console.SetOut( https://msdn.microsoft.com/en-us/library/system.console.setout%28v=vs.110%29.aspx )。
The less insane way to do it would be to use (eg) NLog - then just write Log.Info("message") and have both FileAppender and ConsoleAppender set up :) 不太疯狂的方法是使用(例如)NLog - 然后只写Log.Info(“message”)并同时设置FileAppender和ConsoleAppender :)
Check the NLog here: http://nlog-project.org/ 在这里查看NLog: http : //nlog-project.org/
Relevant config example from https://github.com/NLog/NLog/wiki/Tutorial#multiple-targets : 来自https://github.com/NLog/NLog/wiki/Tutorial#multiple-targets的相关配置示例:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="file.txt" />
<target name="console" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="logfile" />
<logger name="*" minlevel="Info" writeTo="console" />
</rules>
</nlog>
You dont want to store it that way. 你不想那样存储它。 First create the string variable:
首先创建字符串变量:
string val
Then use the string for whatever you want: 然后使用字符串,无论你想要什么:
Console.WriteLine(val);
If you want to log all things written to Console.Out, you can create your own LoggingConsoleWriter that extends TextWriter and writes all text to multiple TextWriters, one of which can be a log-file. 如果要记录写入Console.Out的所有内容,可以创建自己的LoggingConsoleWriter,扩展TextWriter并将所有文本写入多个TextWriters,其中一个可以是日志文件。 Then, you can set the standard output to an instance of this class using the Console.SetOut()-method:
然后,您可以使用Console.SetOut() - 方法将标准输出设置为此类的实例:
// This class writes to two different outputs, one of which can be the log
public class LoggingConsoleWriter : TextWriter
{
private readonly TextWriter _output;
private readonly TextWriter _log;
public LoggingConsoleWriter(TextWriter output, TextWriter log)
{
_output = output;
_log = log;
}
public override void Write(char value)
{
_output.Write(value);
_log.Write(value);
}
public override Encoding Encoding => _output.Encoding;
}
class Program
{
static void Main(string[] args)
{
var logOutput = new StreamWriter(File.OpenWrite("log.txt"));
var newOutput = new LoggingConsoleWriter(Console.Out, logOutput);
// Redirect all Console.WriteX calls to write to newOutput
Console.SetOut(newOutput);
// This call will write to logOutput and the original Console.Out
// through the LoggingConsoleWriter
Console.WriteLine("Hello");
}
}
Alternatively, if you just want to log to a string variable. 或者,如果您只想记录一个字符串变量。 You can instantiate a StringWriter instead, like so:
您可以实例化StringWriter,如下所示:
class Program
{
static void Main(string[] args)
{
var sw = new StringWriter();
var newOutput = new LoggingConsoleWriter(Console.Out, logOutput);
// Redirect all Console.WriteX calls to write to newOutput
Console.SetOut(newOutput);
// This call will write to the StringWriter and the original Console.Out
// through the LoggingConsoleWriter
Console.WriteLine("Hello");
// Retrieve the currently written values
string output = sw.GetStringBuilder().ToString();
}
}
Here's a quick and dirty way to accomplish what you're trying to do using TraceListeners: 这是使用TraceListeners完成您尝试执行的操作的快速而肮脏的方法:
public static void WriteToLog(string _logstring)
{
string filepath = @"C:\myFilePath\fileName"
Trace.Listeners.Clear();
TextWriterTraceListener twtl = new TextWriterTraceListener(filepath + ".log");
ConsoleTraceListener ctl = new ConsoleTraceListener(false);
Trace.Listeners.Add(twtl);
Trace.Listeners.Add(ctl);
Trace.AutoFlush = true;
Trace.WriteLine(_logstring);
}
Then elsewhere in your code you can call this method like this: 然后在代码中的其他地方,您可以像这样调用此方法:
WriteToLog("This will be written to a file and print on the console");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.