简体   繁体   中英

using LoggerFactory() , how to write to file in C#

I have a program , which i need to update. Currently the application , Logger writes to Console. using .AddConsole() method

Kindly can anyone please help me how to write to file. for eg : the "c:\\workspace\\TestProject\\Log.txt"

            logger = new LoggerFactory()
                          .AddConsole()
                          .CreateLogger("Msg");

I guess, you are using Microsoft.Framework.Logging But out-of-the-box implementations are provided for basic console logging and a few other targets, you'll need a logging back-end like Serilog or NLog to gain the kind of functionality you're requesting.

I would recommend you to use NLog (just personal preference)

Install-Package NLog 

then add to your code

loggerFactory.AddNLog(new global::NLog.LogFactory());

https://github.com/aspnet/Logging/tree/dev/samples/SampleApp

http://nlog-project.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM