How to compress log files with NLog - compression

I am using NLog in one of my projects and I am trying to have the output of the files to be compressed.
I tried to use the compress file attribute, but when I look at the files, they are not compressed.
Could you please tell me what I might be doing wrong?
This is my config:
<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" fileName="C:\Workspaces\log.xml"
layout="${message}" keepFileOpen="true"
archiveFileName = "C:\Workspaces\archived\log.{#####}.xml"
archiveAboveSize = "1048576" archiveNumbering = "Sequence"
fileAttributes="Compressed" concurrentWrites = "true"/>
</targets>
<rules>
<logger name ="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>

With NLog 4, you can compress to zip files (.NET 4.5+). See the NLog 4.0 release post
Use enableArchiveFileCompression as follows:
<target name="file" xsi:type="File"
layout="${longdate} ${logger} ${message}"
fileName="${basedir}/logs/logfile.txt"
archiveFileName="${basedir}/archives/log.{#}.zip"
archiveEvery="Day"
archiveNumbering="Rolling"
maxArchiveFiles="7"
enableArchiveFileCompression="true" />

Related

Is it possible that a directory copied and pasted to a particular directory using XHTML plugin in DITA OT 1.8.5

Is it possible that a directory or a file copying from 'resource' folder in org.dita.xhtml to out put folder generated by the XHTML DITA OT transform.
If its possible using xsl changes in plugin its possible means provide me the code.
Any other way is there means please guide me the steps to do.
Please assist me.
You should use the depend.preprocess.post extension point, or another one that fits your needs, to call a new Ant target.
plugin.xml
<plugin id="com.example.extendchunk">
<feature extension="depend.preprocess.post" value="copyfiles"/>
<feature extension="dita.conductor.target.relative" file="myAntStuffWrapper.xml"/>
</plugin>
myAntStuffWrapper.xml
<dummy>
<import file="myAntStuff.xml"/>
</dummy>
myAntStuff.xml
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." name="myAntStuff">
<target name="copyfiles">
<copy todir="foo">
<fileset>
<include name="**/*.bar"/>
</fileset>
</copy>
</target>
</project>

WCF web service Critical Trace

I have a WCF service I want to trace. I have set the next lines int the Web.Config file:
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Critical, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "C:\Logs\MyCriticalTraces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
As you can see I just want CRITICAL messages but the File is extremely large (40 Mb every 5 minutes). It keeps getting larger and larger in a few minutes.
If I look into the file I see many messages but none seem to be Critical.
I just want to trace the Critical errors, otherwise the file would be impossible to move or open.
Any idea about this?
Sorry, I just copied-pasted that tracing code from some forum without knowing what I was doing. Then I realized that the option switchValue was wrong. I mean, I don't need the ActivityTracing log. So the line
switchValue="Critical, ActivityTracing"
Should be:
switchValue="Critical"
So the result is this:
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Critical"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "C:\Logs\MyCriticalTraces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>

Nant wont recognize namespace.Properties

I'm trying to use nant to build a solution for a c# project however I am having an issue specifying resources in my build file. I get the error code:
CS0234: The type or namespace name 'Properties' does not exists in the namespace 'Darkside'. How can I modify my build file to remove the error?
Here is one of the snippets of code in my source files that is causing the issue:
Image enemyImage = DarkSide.Properties.Resources.DarkSide_Asteroid_Medium_Gray;
where Darkside is the namespace of the project, and the rest accesses the resources
Here is a snippet of my build file:
<target name="build" depends="init" description="compiles the source code">
<property name="build.dir" value="${project::get-base-directory()}/${project::get-name()}/bin/release/${project.version}${basedir.suffix}"/>
<mkdir dir="${build.dir}"/>
<csc target="exe" output="${build.dir}/Darkside.exe" debug="${build.debug}">
<sources>
<include name="Darkside/*.cs"/>
</sources>
<resources >
<include name="${project::get-base-directory()}/${project::get-name()}/Properties/**" />
</resources>
</csc>
</target>
You should probably define the prefix for the resources tag (with dynamicprefix="true").
<target name="build" depends="init" description="compiles the source code">
<property name="build.dir" value="${project::get-base-directory()}/${project::get-name()}/bin/release/${project.version}${basedir.suffix}"/>
<mkdir dir="${build.dir}"/>
<csc target="exe" output="${build.dir}/Darkside.exe" debug="${build.debug}">
<sources>
<include name="Darkside/*.cs"/>
</sources>
<resources dynamicprefix="true" prefix="DarkSide">
<include name="${project::get-base-directory()}/${project::get-name()}/Properties/**" />
</resources>
</csc>
</target>
Indeed the csc task's documentation states:
Note: In order to have <csc> task generate manifest resource names that match those generated by Microsoft Visual Studio.NET, the value of the prefix attribute of the <resources> element should match the "Default Namespace" of the C# project, and the value of the dynamicprefix attribute should be set to "true".

Compressing a web service response for jQuery

I'm attempting to gzip a JSON response from an ASMX web service to be consumed on the client-side by jQuery.
My web.config already has httpCompression set like so: (I'm using IIS 7)
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"
staticCompressionDisableCpuUsage="90" staticCompressionEnableCpuUsage="60"
dynamicCompressionDisableCpuUsage="80" dynamicCompressionEnableCpuUsage="50">
<dynamicTypes>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="application/x-javascript" enabled="true"/>
<add mimeType="text/css" enabled="true"/>
<add mimeType="video/x-flv" enabled="true"/>
<add mimeType="application/x-shockwave-flash" enabled="true"/>
<add mimeType="text/javascript" enabled="true"/>
<add mimeType="text/*" enabled="true"/>
<add mimeType="application/json; charset=utf-8" enabled="true"/>
</dynamicTypes>
<staticTypes>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="application/x-javascript" enabled="true"/>
<add mimeType="text/css" enabled="true"/>
<add mimeType="video/x-flv" enabled="true"/>
<add mimeType="application/x-shockwave-flash" enabled="true"/>
<add mimeType="text/javascript" enabled="true"/>
<add mimeType="text/*" enabled="true"/>
</staticTypes>
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
</httpCompression>
<urlCompression doDynamicCompression="true" doStaticCompression="true"/>
Through fiddler I can see that normal aspx and other compressions work fine. However, the jQuery ajax request and response work as they should, only nothing gets compressed.
What am I missing?
You can change httpCompression only in applicationHost.config.
See this link
Like you, I tried changing it in web.config first, but it didn't work. It worked only when I added the following lines to C:\Windows\System32\inetsrv\config\applicationHost.config:
<dynamicTypes>
...
<add mimeType="application/json" enabled="true" />
<add mimeType="application/json; charset=utf-8" enabled="true" />
...
</dynamicTypes>
DO USE NOTEPAD to edit applicationHost.config. I've wasted several hours before understood that my changes made in notepad++ (as well as in Visual Studio 2010 editor!!) aren't applied by IIS.
Alternative way to add additional mimeType into dynamicTypes/staticTypes collection is to use appcmd. "C:\Windows\System32\Inetsrv\Appcmd.exe" set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/javascript',enabled='True']" /commit:apphost
And again: after these changes made - you'll see them only in notepad. Notepad++ (as well as Visual Studio 2010 editor!!) maintains some kind of f*ing alternate reality/storage for applicationHost.config. It shows you his own version of the file (different from the one you see in notepad) even after the file edited in notepad and reopened in np++/VS.
Eric P's answer is mostly correct... you need to EXACTLY match the Content-Type header sent by IIS in its HTTP Response Headers. For some reason our IIS7 server was responding with:
Content-Type: application/json; q=0.5
I had never ever observed a quality factor in a server response before. How bizarre.
When we added this to the dynamicTypes in the .config file everything started working:
<dynamicTypes>
...
<add mimeType="application/json" enabled="true" />
<add mimeType="application/json; q=0.5" enabled="true" />
<add mimeType="application/json; charset=utf-8" enabled="true" />
<add mimeType="application/json; q=0.5; charset=utf-8" enabled="true" />
...
</dynamicTypes>
Changes in web.config don't work because of the following line in applicationHost.config:
<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
If you replace it by:
<section name="httpCompression" overrideModeDefault="Allow" />
the changes are possible locally.
I think this is more convenient as you are able to configure every service differently and you don't have to edit your applicationHost.config if you must add a new MIME type.
Here is an example how to activate compression in web.config on a single ASMX service located in the service subfolder:
<location path="service/MySpecificWebService.asmx">
<system.webServer>
<httpCompression>
<dynamicTypes>
<add mimeType="application/json" enabled="true" />
<add mimeType="application/json; charset=utf-8" enabled="true" />
</dynamicTypes>
</httpCompression>
<urlCompression doDynamicCompression="true" />
</system.webServer>
</location>
Concerning the actual editing of applicationHost.config, I suspect that it is not a true file in the file system. If you copy that file on your desktop, you will be able to edit it with any text editor, then copy it back to its original folder.
http://forums.iis.net/t/1160210.aspx?missing+applicationhost+config
The config file is supposed to be %windir%\system32\inetsrv\config\applicationhost.config.
(Note that if your application (which is searching for applicationhost.config) is a 32bit app (for example, if you're using a 32bit CMD.EXE), then you won't be able to see the configuration file due to Windows SYSWOW32 redirection)
A bit of explanation regarding the missing applicationhost.config to change overrideModeDefault attribute to Allow. It's due to SYSWOW32 redirection.
Also you may not see the configuration files until you
Open the folder by pasting "%windir%\system32\inetsrv\config\" exactly in the File Explorer location bar, and not your text editor
Right-click and edit the file directly in that folder.
This is because for some reason some 64bit editors still use a faulty file-chooser dialog somehow.

Log4Net "Could not find schema information" messages

I decided to use log4net as a logger for a new webservice project. Everything is working fine, but I get a lot of messages like the one below, for every log4net tag I am using in my web.config:
Could not find schema information for
the element 'log4net'...
Below are the relevant parts of my web.config:
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level: %message%newline" />
</layout>
</appender>
<logger name="TIMServerLog">
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</logger>
</log4net>
Solved:
Copy every log4net specific tag to a separate xml-file. Make sure to use .xml as file extension.
Add the following line to AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "xmlFile.xml", Watch = true)]
nemo added:
Just a word of warning to anyone
follow the advice of the answers in
this thread. There is a possible
security risk by having the log4net
configuration in an xml off the root
of the web service, as it will be
accessible to anyone by default. Just
be advised if your configuration
contains sensitive data, you may want
to put it else where.
#wcm: I tried using a separate file. I added the following line to AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
and put everything dealing with log4net in that file, but I still get the same messages.
You can bind in a schema to the log4net element. There are a few floating around, most do not fully provide for the various options available. I created the following xsd to provide as much verification as possible:
http://csharptest.net/downloads/schema/log4net.xsd
You can bind it into the xml easily by modifying the log4net element:
<log4net
xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
I had a different take, and needed the following syntax:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.xml", Watch = true)]
which differs from xsl's last post, but made a difference for me. Check out this blog post, it helped me out.
Just a word of warning to anyone follow the advice of the answers in this thread. There is a possible security risk by having the log4net configuration in an xml off the root of the web service, as it will be accessible to anyone by default. Just be advised if your configuration contains sensitive data, you may want to put it else where.
I believe you are seeing the message because Visual Studio doesn't know how to validate the log4net section of the config file. You should be able to fix this by copying the log4net XSD into C:\Program Files\Microsoft Visual Studio 8\XML\Schemas (or wherever your Visual Studio is installed). As an added bonus you should now get intellisense support for log4net
In Roger's answer, where he provided a schema, this worked very well for me except where a commenter mentioned
This XSD is complaining about the use of custom appenders. It only allows for an appender from the default set (defined as an enum) instead of simply making this a string field
I modified the original schema which had a xs:simpletype named log4netAppenderTypes and removed the enumerations. I instead restricted it to a basic .NET typing pattern (I say basic because it just supports typename only, or typename, assembly -- however someone can extend it.
Simply replace the log4netAppenderTypes definition with the following in the XSD:
<xs:simpleType name="log4netAppenderTypes">
<xs:restriction base="xs:string">
<xs:pattern value="[A-Za-z_]\w*(\.[A-Za-z_]\w*)+(\s*,\s*[A-Za-z_]\w*(\.[A-Za-z_]\w*)+)?"/>
</xs:restriction>
</xs:simpleType>
I'm passing this back on to the original author if he wants to include it in his official version. Until then you'd have to download and modify the xsd and reference it in a relative manner, for example:
<log4net
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../Dependencies/log4net/log4net.xsd">
<!-- ... -->
</log4net>
Actually you don't need to stick to the .xml extension. You can specify any other extension in the ConfigFileExtension attribute:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension=".config", Watch = true)]
#steve_mtl: Changing the file extensions from .config to .xml solved the problem. Thank you.
#Wheelie: I couldn't try your suggestion, because I needed a solution which works with an unmodified Visual Studio installation.
To sum it up, here is how to solve the problem:
Copy every log4net specific tag to a separate xml-file. Make sure to use .xml as file extension.
Add the following line to AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "xmlFile.xml", Watch = true)]
For VS2008 just add the log4net.xsd file to your project; VS looks in the project folder as well as the installation directory that Wheelie mentioned.
Also, using a .config extension instead of .xml avoids the security issue since IIS doesn't serve *.config files by default.
Have you tried using a separate log4net.config file?
I got a test asp project to build by puting the xsd file in the visual studio schemas folder as described above (for me it is C:\Program Files\Microsoft Visual Studio 8\XML\Schemas) and then making my web.config look like this:
<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<appSettings>
</appSettings>
<connectionStrings>
</connectionStrings>
<system.web>
<trace enabled="true" pageOutput="true" />
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true" />
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows" />
<customErrors mode="Off"/>
<!--
<customErrors mode="Off"/>
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<!-- Please make shure the ..\\Logs directory exists! -->
<param name="File" value="Logs\\Log4Net.log"/>
<!--<param name="AppendToFile" value="true"/>-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="" />
<from value="" />
<subject value="" />
<smtpHost value="" />
<bufferSize value="512" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property] - %message%newline%newline%newline" />
</layout>
</appender>
<logger name="File">
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</logger>
<logger name="EmailLog">
<level value="ALL" />
<appender-ref ref="SmtpAppender" />
</logger>
</log4net>
</configuration>
Without modifying your Visual Studio installation, and to take into account proper versioning/etc. amongst the rest of your team, add the .xsd file to your solution (as a 'Solution Item'), or if you only want it for a particular project, just embed it there.
I noticed it a bit late, but if you look into the examples log4net furnishes you can see them put all of the configuration data into an app.config, with one difference, the registration of configsection:
<!-- Register a section handler for the log4net section -->
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
Could the definition it as type "System.Configuration.IgnoreSectionHandler" be the reason Visual Studio does not show any warning/error messages on the log4net stuff?
I followed Kit's answer https://stackoverflow.com/a/11780781/6139051 and it didn't worked for AppenderType values like "log4net.Appender.TraceAppender, log4net". The log4net.dll assembly has the AssemblyTitle of "log4net", i.e. the assembly name does not have a dot inside, that was why the regex in Kit's answer didn't work. I has to add the question mark after the third parenthetical group in the regexp, and after that it worked flawlessly.
The modified regex looks like the following:
<xs:pattern value="[A-Za-z_]\w*(\.[A-Za-z_]\w*)+(\s*,\s*[A-Za-z_]\w*(\.[A-Za-z_]\w*)?+)?"/>