Quartz.Net Jobs to Log File - c#

I'm currently creating a scheduler using Quartz.net with the CronSchedule. I'm wanting to send information regarding the 'Jobs' such as time executed and finished to a .txt file or something which logs the information, however I don't know how to approach this.
Any help?

You can use log4net, I suggest to log separate for each Job.
Before to start scheduler:
var location = System.Reflection.Assembly.GetExecutingAssembly().Location;
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(location + ".config"));
In each class that extends IJob (so, in each Job):
var logger = log4net.LogManager.GetLogger("TestJobLogger");
Where obviously "TestJobLogger" is different for each Job.
In the App.config insert this, one <logger> and one <appender> for each Job you have:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
...
<log4net>
<appender name="AppenderTestJob" type="log4net.Appender.RollingFileAppender">
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value=".yyyyMMdd-HH" />
<param name="ImmediateFlush" value="true" />
<maxSizeRollBackups value="10" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="C:\PathToSaveFile\" />
<datePattern value="'test.'yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd-MM-yyyy HH:mm:ss.fff} [%t] [%-5p] (%c) - %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
</root>
<logger name="TestJobLogger" additivity="false">
<level value="INFO" />
<appender-ref ref="AppenderTestJob" />
</logger>
</log4net>
Now, you can log simply with logger.Debug("TESTTTTTT");.

Related

In Log4Net all specified log folders are created but logging is not working for File Appender

I'm using Castle with Log4Net. All folders were created but logging information is not written to a text file.
Here is the config:
<add key="log4net.Internal.Debug" value="true"/>
<add key="log4net.Internal.Error" value="true"/>
<add key="log4net.Internal.Info" value="true"/>
<add key="log4net.Internal.Fatal" value="true"/>
<add key="log4net.Internal.Warn" value="true"/>
<appender name="debugAppender" type="log4net.Appender.RollingFileAppender,log4net">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="DEBUG" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<file value="..\\..\\LOGS\\DEBUG\\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<DatePattern value="yyyy\\\\MM\\\\dd\\\\yyyyMMdd'_debug.txt'" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="50MB" />
<countDirection value="1" />
<preserveLogFileNameExtension value="true" />
<encoding value="utf-8" />
<!--<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />-->
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>`
<logger name="Log">
<appender-ref ref="errorAppender" />
<appender-ref ref="infoAppender" />
<appender-ref ref="debugAppender" />
<appender-ref ref="warningAppender" />
<appender-ref ref="fatalAppender" />
</logger>`
I'm using console application for this, and the Main method will start like this:
XmlConfigurator.Configure(new FileInfo("log4net.config"));
And everyone has permission to write, read all files in LOGS folders.
I used your same configuration of course with little modification which you can try
I created a console application as yours:
Here is my Entry point:
public class Program
{
private static readonly log4net.ILog log
= log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
XmlConfigurator.Configure(new FileInfo("..\\..\\log4net.config"));
log.Debug("Hi this is DEBUG from logging");
log.Info("This is information from logger");
log.Error("This is Error from logger");
Console.ReadLine();
}
}
And here is the configuration I used:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net"/>
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender,log4net">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE}
[%thread] %level %logger - %message%newline"/>
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="DEBUG" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="debugAppender" type="log4net.Appender.RollingFileAppender,log4net">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="DEBUG" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<file value="..\\..\\LOGS\\DEBUG\\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<DatePattern value="yyyy\\\\MM\\\\dd\\\\yyyyMMdd'_debug.txt'" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="50MB" />
<countDirection value="1" />
<preserveLogFileNameExtension value="true" />
<encoding value="utf-8" />
<!--<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />-->
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
<!--<appender-ref ref="DebugOutputAppender" />-->
<appender-ref ref="debugAppender" />
</root>
</log4net>
</configuration>
You can notice I have added Console appender to check whether the logging is working as expected on the console before checking the log file.
Note: With the same configuration as yours I was not able to log any information in Console as well as file although the folder and the file were getting created without any issues.
The change I did is, added a root level element as pasted in the above configuration snippet.
You need to have one root section to house your top-level logger references. These are the loggers that inherit information from your base logger (root).
I have removed the additional loggers which you have used.
Sometimes you will want to know more about a particular part of your application. log4net anticipated this by allowing you to specify additional logger references beyond just the root logger. For example, here is an additional logger to log to the console messages that occur inside the OtherClass class object:
<logger name="Log4NetTest.OtherClass">
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
</logger>
Use it if you are really sure on this.
Good tutorial to start with log4net is here.
Let me know if you face any issues.

How to configure log4net to log into multiple files

I have a windows service application which is basically a solution created with VS2015 and contains 2 projects.
The first project is dealing with the service itself (start, stop, etc) while the second one is maintaining the process that will be executed due to launch of the service.
I am using the log4net to log messages into a file.
I wanted to created 2 separate log files for each project.
This has been configured into the service project and is up and running.
while it is not working for the second project.
So mainly my question is how to configure log4net to log into different files within one solution?
Configure log4net within the executing project only. If you want log messages from an specific assembly logged to a separate file make sure the root namespace for that assembly is unique and define two log4net appenders, each logging to a separate file and use logging filters to filter by namespace.
<!-- Will log only from the My.Process namespace -->
<appender name="ProcessLog" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="My.Process" />
<acceptOnMatch value="true" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<file value="process.log" />
<!-- layout, etc -->
</appender>
<!-- will log everything except from the My.Process namespace -->
<appender name="ServiceLog" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="My.Process" />
<acceptOnMatch value="false" />
</filter>
<file value="service.log" />
<!-- layout, etc -->
</appender>
And don't forget to add a reference to both appenders in the root element.
Assuming your first and second project have different root namespaces (e.g. Project1.Service and Project2.MaintainProcess), you can use 2 appenders and the logger hierarchy to split the log files by project:
<log4net>
<appender name="Project1Logger" type="log4net.Appender.RollingFileAppender">
<file value="Project1Logger.log" />
</appender>
<appender name="Project2Logger" type="log4net.Appender.RollingFileAppender">
<file value="Project2Logger.log" />
</appender>
<root>
<level value="ALL" />
</root>
<logger name="{Project1RootNamespaceHere}">
<appender-ref ref="Project1Logger" />
</logger>
<logger name="{Project2RootNamespaceHere}">
<appender-ref ref="Project2Logger" />
</logger>
</log4net>
If you don't want to use separate appenders and logger hierarchy then see this answer for an alternative method using a custom pattern substitution to use a single appender. I'm not sure of the performance implications of that approach though.
Complete configuration (with a MinimalLock file appender):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<log4net>
<!-- ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
<appender name="RollingFileAppender_Info" type="log4net.Appender.RollingFileAppender">
<file value="Info.log" />
<encoding value="utf-8" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message %exception%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="WARN" />
</filter>
</appender>
<appender name="RollingFileAppender_Error" type="log4net.Appender.RollingFileAppender">
<file value="Error.log" />
<encoding value="utf-8" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message %exception%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender_Info" />
<appender-ref ref="RollingFileAppender_Error" />
</root>
</log4net>
</configuration>

log4net not logging errors and fatal in separate file

I tried to separate the errors and fatal into separate log file not i dont see it logging in separate file. here is my config file. can you point what mistake i made?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\NinjaTraderLogs\NinjaLogs.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level (%property{method}) (%property{state}) (%property{instrument}) - %message%newline" />
</layout>
<level value="All" />
</appender>
<appender name="FatalError" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\NinjaTraderLogs\NinjaLogs_Fatal.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level (%property{method}) (%property{state}) (%property{instrument}) - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
Create one more appender ref as below under <root>
<root>
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="FatalError" />
</root>
Before you call your logging in code make sure to call the XmlConfigurator function Configure (call it globally) like
log4net.Config.XmlConfigurator.Configure();
Last but not least, make sure you have write permission to the directory you are trying to create log file. I would suggest you to check windows event log once Application log to see if any error has happened.

log4net:ERROR Failed to find configuration section 'log4net'

I get this error when I try to configure my logger using:
log4net.Config.XmlConfigurator.Configure();
The app.config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="C:\Logs\my-log-file.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger [%C{1}.%M] - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
I'm trying to write separate logs for each component of a system. I have one working for the main class but I get this error when trying to configure another one for a different class. What is the cause?
Thanks in adv!
I ended up using the following to configure my Add-In and RTD:
[Add-In]
log4net.Config.XmlConfigurator.Configure(
new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "MyAdd-In.dll.config"));
[RTD]
log4net.Config.XmlConfigurator.Configure(
new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\RtdServer.dll.config"));
Which then configured the separate logger for my RTD.
Hth

Cancel console printing

I have the following config file:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="D:\log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="20" />
<maximumFileSize value="4MB" />
<staticLogFileName value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
Every line I am logging is written to Console as well, how can I cancel it?
I want it to be appended to the log.txt file ONLY, without anything in Console.
Either you have added console appender programmatically, or this configuration file is not used by log4net. Log4net don't add any default appenders (like default TraceListener in .net). And this configuration specifies only RollingFileAppender. So, verify which configuration file is used, and whether some appenders added in the code.

Categories