When I set the file value to logs\log-file.txt
, where exactly will it create this folder? In the /bin
directory?
My web.config looks like this:
<log4net> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="logs\log-file.txt" /> <appendToFile value="true" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> </log4net>
Is this the correct way to log:
ILog logger = LogManager.GetLogger(typeof(CCController)); logger.Error("Some Page", ex); // where ex is the exception instance
In your case, the log file will be in bin\Debug\netcoreapp3.
You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.
Log4net watches for any new file created in the folder so simply creating the . log4net configuration file triggers the update within the component that is logging. When using a file appender, the destination folder does not have to exist. Log4net creates the folder.
If you want your log file to be place at a specified location which will be decided at run time may be your project output directory then you can configure your .config
file entry in that way
<file type="log4net.Util.PatternString" value="%property{LogFileName}.txt" />
and then in the code before calling log4net configure
, set the new path like below
log4net.GlobalContext.Properties["LogFileName"] = @"E:\\file1"; //log file path log4net.Config.XmlConfigurator.Configure();
How simple is it? :)
it will create the file in the root directory of your project/solution.
You can specify a location of choice in the web.config of your app as follows:
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="c:/ServiceLogs/Olympus.Core.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value=".yyyyMMdd.log" /> <maximumFileSize value="5MB" /> <staticLogFileName value="true" /> <lockingModel type="log4net.Appender.RollingFileAppender+MinimalLock" /> <maxSizeRollBackups value="-1" /> <countDirection value="1" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %-5level [%thread] %logger - %message%newline%exception" /> </layout> </appender>
the file tag specifies the location.
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