I am using Nlog in my MVC project
What I want to achieve is to log a message when a action method is executed in my controller. and the log message should appear on a txt file. But I cant seem to get it to work
I have installed Nlog to my references.
I've got Nlog.Config with following configuration:
<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="file"
xsi:type="File"
layout="${longdate} ${logger} ${message}"
fileName="${basedir}/logs/logfile.txt"
keepFileOpen="false"
encoding="iso-8859-2" />
</targets>
<rules>
<logger name="*"
minlevel="Debug"
writeTo="file" />
</rules>
</nlog>
and then inside my POST action method in my controller I have following code:
[HttpPost]
public ActionResult Edit(int id, CreateNKIphase1ViewModel model)
{
Logger logger = LogManager.GetCurrentClassLogger();
logger.Info("Test message");
//code....
}
Where can I find the txt file?
Why is this not working :S
If your application is running in IIS using an application pool identity, you will need to grant folder permissions to that identity.
When you enter the name for the app pool identity, you will need to include IIS APPPOOL
. For example, if the app pool identity is ASP.NET v4.0
, enter the name as IIS APPPOOL\ASP.NET v4.0
. Also, make sure the location is the local machine, not a network domain.
Why not set the fileName
attribute to a known path and name, rather than using the ${basedir}
format?
fileName="C:\Temp\logfile.txt"
That way you will know where the file is supposed to be. Once you have that part of the logging down, you can start adding in the more advanced techniques.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With