nlog files with a date - c#-3.0

In a C# 2008 application, I want to write the NLog error log files out so that the files have a date on them. In the example listed below, you can see that I have 3 log files. Can you tell me how place a month-day-year format on these files?
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="C:\Logs\NlogOutput.log" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
</rules>
</nlog>

Use something like this for your fileName parameter:
fileName="${basedir}/${shortdate}.log"
Generally speaking, you can use most NLog LayoutRenderers to help compose your filename.

Related

NLog not outputting anything to console

I'm following this tutorial
https://www.youtube.com/watch?v=jn6X-rx78ZU&list=PLJFgzBCcspK8p7Hxu2OLh-f-x0PBsIGjH&index=15&ab_channel=TheTutorialChef
and I'm trying to run the server at the end. NLog should output to console that server has started, but NLog is not outputting anything. I added the NLog config file below.
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<target name="Console" xsi:type="Console" layout=" ${level} ${date} ${message} ${exception:innerFormat=Message,StackTrace}" />
<!-- https://github.com/NLog/NLog/wiki/ColoredConsole-target -->
<target name="CConsole" useDefaultRowHighlightingRules="true" xsi:type="ColoredConsole" layout="${logger} ${level} ${date} ${message} ${exception:innerFormat=Message,StackTrace}" >
</target>
<target name="warnfile" xsi:type="File" fileName="${basedir}/Logs/warning.txt"
maxArchiveFiles="4"
archiveAboveSize="10240"
archiveEvery="Day" />
<target name="infofile" xsi:type="File" fileName="${basedir}/Logs/info.txt"
maxArchiveFiles="4"
archiveAboveSize="10240"
archiveEvery="Day" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Info" maxlevel="Warn" writeTo="infofile" />
<logger name="*" minlevel="Warn" writeTo="warnfile" />
<logger name="*" minlevel="Trace" writeTo="CConsole" />
</rules>
</nlog>
But nothing outputs. There are no compilation errors or warnings. What have I done wrong? I'm using VSCode 2017, and NLog 4.7.6. I've tried editing the cofig file in the .nuget package directly. I've tried reinstalling NLog to get the original NLog.config, but now when I reinstall NLog, the config file doesn't appear in the root directory again.

IIS 10 - web.config - how to enable default document without script access

We have a folder which contains only static html and images etc. No scripts should be allowed to execute from within this folder. However we would still like to be able to use html default documents.
What is the correct way to configure this?
This is the web.config file...
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers accessPolicy="Read"/>
<defaultDocument enabled="true">
<files>
<clear />
<add value="default.html" />
<add value="default.htm" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
If I attempt to access http://mysite/mystaticfolder/ it fails with the error...
HTTP Error 403.1 - Forbidden
However the URL http://mysite/mystaticfolder/default.html works fine.
Surely it shouldn't be nescessary to allow dynamic scripts, just to be able to serve static html default documents?
In case it helps anyone, I've been able to solve it with the following...
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers accessPolicy="Read">
<clear/>
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule" resourceType="Either" requireAccess="Read" />
</handlers>
<defaultDocument enabled="true">
<files>
<clear />
<add value="default.html" />
<add value="default.htm" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
I'm not entirely sure though why this doesn't work by default though.

How to configure Quartz.Net version 2.1.2.0 integrated with Log4Net

I have downloaded the newest version of Quartz.Net and try to configure it with common.logging and log4net dlls by using below configuration (based on this example)
<configSections>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=2.1.2.400,Culture=neutral, PublicKeyToken=f6b8c98a402cc8a4" />
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging, Version=2.1.2.0, Culture=neutral, PublicKeyToken=af08829b84f0328e" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
<arg key="configType" value="INLINE" />
</factoryAdapter>
</logging>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="MyQuartzLog.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="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
<quartz>
<add key="quartz.scheduler.instanceName" value="QuartzTestLog4Net" />
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
<add key="quartz.threadPool.threadCount" value="10" />
<add key="quartz.threadPool.threadPriority" value="2" />
<add key="quartz.jobStore.misfireThreshold" value="60000" />
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />
</quartz>
It does not work.
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=2.1.2.400,Culture=neutral, PublicKeyToken=f6b8c98a402cc8a4" />
results in component loading error. I used sn.exe to check the publickeytoken. The keytoken is correct.
Removing the quartz sction can make the application run. But the log object has NoOpLogger information - {Common.Logging.Simple.NoOpLogger}.
My project refers to Common.Logging version 2.1.2.0; Common.Logging.Log4Net version 2.0.0.0; and log4net version 1.2.10.0. Are they right versions? If not, what are the right ones.
Thanks,
You should check the sample server that comes with Quartz.NET distribution.
https://github.com/quartznet/quartznet/tree/master/server/Quartz.Server
App.config:
It's type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"
NameValueSectionHandler is BCL type, not Quartz.NET's
You can probably leave out the version and public key
Package.config
You might have wrong packages. packages.config in sample contains working combination when using Log4Net 1.2.11

External Config Files with elmah

I am using elmah (v1.1.11517.0) and am trying to move the config to an external source.
My config currently looks like this:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
<section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah"/>
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net configSource="Settings\RobDev\log4net.config" />
<elmah configSource="Settings\RobDev\ELMAH.config" />
</configuration>
log4net is happy and runs fine, however for elmah I get the error
Parser Error Message: The attribute 'configSource' cannot be specified because its name starts with the reserved prefix 'config' or 'lock'.
Which is a pain, the elmah file is definitely there, but something isn't happy.
What might be causing this?
The reason why you can't use the configSource element for elmah is because elmah is defined as a sectionGroup. You can use the configSource on Sections. That is why it works on log4net.
If you need the seperate config-file for web-deployment like Dan Atkinson you could do the following:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
<section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah"/>
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net configSource="Settings\RobDev\log4net.config" />
<elmah>
<errorLog configSource="Settings\RobDev\errorLog.config" />
<errorMail configSource="Settings\RobDev\errorMail.config" />
<errorFilter configSource="Settings\RobDev\errorFilter.config" />
<errorTweet configSource="Settings\RobDev\errorTweet.config" />
<security configSource="Settings\RobDev\security.config" />
</elmah>
</configuration>
The downside is that you need a config file for each section. But you often do that for web deployment projects.
I've added a bounty to this question as I'd also like to know the answer to this.
I need it because I use Web Deployment functionality which replaces files by their configSource attribute.
In the meantime, you could move the contents of elmah.config into your web.config, replacing the <elmah configSource="..." />.

NHAML Directives

Does anyone know how to use directives (ex. # Import) with NHAML?
if you mean adding references and importing namespaces you do it in the app.config or web.config
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="nhaml" type="NHaml.Configuration.NHamlConfigurationSection, NHaml"/>
</configSections>
<nhaml autoRecompile="true">
<assemblies>
<add assembly="NHaml.Samples.Mvc"/>
</assemblies>
<namespaces>
<add namespace="NHaml.Web.Mvc"/>
<add namespace="NHaml.Samples.Mvc.Controllers"/>
</namespaces>
</nhaml>
...
</configuration>