Hi, Is it possible to logging the error log into Local drive either ( c: or d: ) after hosting the application in iis . if anyone knows this,
suggest me with example code.
Thanking you,
Karthik.K
Hi, Is it possible to logging the error log into Local drive either ( c: or d: ) after hosting the application in iis . if anyone knows this,
suggest me with example code.
Thanking you,
Karthik.K
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Jul 10, 2023, 5:36 AM EDT
It is generally not possible to directly save logs to the end user's local system using NLog or any other server-side logging framework. When hosting an application in IIS, the application runs on the server, and the log files are stored on the server's file system.
Deepak RawatRank #646 · 3.7K reputationPosted Jul 10, 2023, 3:33 AM EDT
In your ASP.NET Core project, open the
appsettings.jsonfile and add a new section for logging configuration{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" }, "FilePath": "C:\\Logs\\error.log" // Update the file path as per your requirement }, // Other configuration settings... }we have specified the file path as
"C:\\Logs\\error.log"for logging errors to theerror.logfile in theC:\Logsdirectory. You can change the file path and name as per your preference.Startup.csfile, configure logging in theConfigureServicesmethodusing Microsoft.Extensions.Logging; public class Startup { public void ConfigureServices(IServiceCollection services) { // Other configuration... services.AddLogging(builder => { builder.AddConfiguration(Configuration.GetSection("Logging")); builder.AddFile(options => { options.FileName = Configuration["Logging:FilePath"]; }); }); // Other services... } // Other methods... }we are using the
AddFileextension method from a third-party logging provider (such asSerilog.Extensions.Logging.File). Make sure you have the necessary NuGet package installed for the file logging functionality.ILoggerinto your classes where you want to log errors and use it to log errorsusing Microsoft.Extensions.Logging; public class YourClass { private readonly ILogger _logger; public YourClass(ILogger logger) { _logger = logger; } public void SomeMethod() { try { // Your code that may throw an exception } catch (Exception ex) { _logger.LogError(ex, "An error occurred"); } } }we inject
ILoggerinto the constructor ofYourClassand then use_logger.LogErrorto log an error with the exception details and a message.With the above configuration, any errors logged using the logger will be written to the specified file path (
C:\Logs\error.login our example) on the local drive. Make sure the application has appropriate write permissions to create and write to the specified file path.Karthik KRank #2,589 · 538 reputationPosted Jul 10, 2023, 3:43 AM EDT
@ Rawat ,
Thanks for the quick reply. currently we have using the asp,net. Not a Asp.net core application. could you please suggest me the in asp.net?
Thanks
Amit MohantyRank #112 · 20K reputationPosted Jul 10, 2023, 4:49 AM EDT
nlog.config:In your code files, you can now use NLog to log messages. For example:
using NLog; private static readonly Logger logger = LogManager.GetCurrentClassLogger(); // Log an error logger.Error("An error occurred.", exception); // Log an informational message logger.Info("Informational message.");Karthik KRank #2,589 · 538 reputationPosted Jul 10, 2023, 5:12 AM EDT
@amit ,
will it write into end user local System ? after we host the appplication in IIS ? i would like to have like this .
to write the error into logfile.even after host the webpplication in iis.
Thanks