Using registry values in log4net configuration - c#

Is it possible to put a registry value in the log4net filename in the configuration file?
for example, something like this:
<log4net>
<appender name="MainLog" type="log4net.Appender.RollingFileAppender">
<param name="Encoding" value="Unicode" />
<param name="File" value="Logs\{Some-Registry-Value}\myLog.log" />
<param name="AppendToFile" value="false" />
<maximumFileSize value="50MB" />
<maxSizeRollBackups value="20" />
<rollingStyle value="Size"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %logger - %m%n%exception" />
</layout>
</appender>

You can use a custom property in your xml so your line would look like:
<param name="File" value="Logs\%property{RegLogPath}\myLog.log" />
Then in code, before configuring log4net:
log4net.GlobalContext.Properties["RegLogPath"] = logsFolder;

The accepted answer doesn't work for me - instead it creates the log files in a directory that is literally named "%property{RegLogPath}"!
Maybe this syntax is not quite right?
EDIT:
As noted here: log4net %property does not work
...it appears the parameter 'type="log4net.Util.PatternString"' (but not the '.\' before '%property') is needed in the config file.
With this change, it works.

Related

log4net not appending to azure blob

I want to append logs to azure blob storage. So i added nuget package called log4net.appender.azure.
Wrote following in app.config
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="AzureBlobAppender" />
</root>
<appender name="AzureBlobAppender" type="log4net.Appender.AzureBlobAppender, log4net.Appender.Azure">
<param name="ContainerName" value="myConatiner"/>
<param name="DirectoryName" value="myFolder/logs.txt"/>
<param name="ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=XXXX;AccountKey=Ngqa/KvLxL4zpxdPDv8Opm29JCOXTJuJsF8FrzFQZpWCOcoFm1EI+mvFu+7AJvaWEU3jDffYrf4rGOKPJu/ObA==;EndpointSuffix=core.windows.net" />
<param name="AsText" value="true" />
</appender>
this is how i defined connection string
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
then i wrote below in class file
log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Info("I am being tested");
but when i run program i dont see anything logged in azure blob. What am i missing?
Looks to me like your configuration is wrong. According to the examples in the README...
<appender name="AzureBlobAppender" type="log4net.Appender.AzureBlobAppender, log4net.Appender.Azure">
<param name="ContainerName" value="testloggingblob"/>
<param name="DirectoryName" value="logs"/>
<!-- You can either specify a connection string or use the ConnectionStringName property instead -->
<param name="ConnectionString" value="UseDevelopmentStorage=true"/>
<!--<param name="ConnectionStringName" value="GlobalConfigurationString" />-->
<param name="AsText" value="true" />
</appender>
myContainer should be ContainerName
MyFolder should be DirectoryName
AzureBlobConnectionString should be ConnectionStringName
Try following steps and proceed to next only if previous one is fine.
You need to check if the Logger property in debugger mode and examine the ConfigurationMessages property to see if any errors are reported.
First try adding a file appender to see if log4net is working fine or not.
<appender name="fileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<maximumFileSize value="1000KB" />
<maxSizeRollBackups value="10" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger - %message%newline" />
</layout>
</appender>
With this, you can go to Kudo in Azure portal's webapp and see the physical file being written atleast.
Second, see if you are initializing the config properly with
log4net.Config.XmlConfigurator.Configure();
Third, check if the region of the storage account is the same as the web app or not.

How can I log into a text file(not xml) on my Azure BlobStorage using log4net?

So I'm already logging (log4net) on my own machine using the following configuration
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="mylog.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger [%M %C] - %message%newline" />
</layout>
</appender>
Just to test log4net and make sure everything is working, now I want to log on my Azure blob storage, I'm using log4net.Appender.Azure nuget package and it did log to my blob storage using this configuration:
<appender name="AzureBlobAppender" type="log4net.Appender.AzureBlobAppender, log4net.Appender.Azure">
<param name="ContainerName" value="testblob" />
<param name="DirectoryName" value="logs/" />
<!-- You can either specify a connection string or use the ConnectionStringName property instead -->
<param name="ConnectionString" value="UseDevelopmentStorage=true;" />
<!--<param name="ConnectionStringName" value="GlobalConfigurationString" />-->
<bufferSize value="5" />
</appender>
But this configuration left me with many XML files instead of ONE text file so is there a way to do that? I searched around but found nothing related to this.
EDIT: So to append the xml into only one can be done using the following configuration
<appender name="AzureAppendBlobAppender" type="log4net.Appender.AzureAppendBlobAppender, log4net.Appender.Azure">
<param name="ContainerName" value="testloggingblob"/>
<param name="DirectoryName" value="logs"/>
<!-- You can either specify a connection string or use the ConnectionStringName property instead -->
<param name="ConnectionString" value="UseDevelopmentStorage=true"/>
<!--<param name="ConnectionStringName" value="GlobalConfigurationString" />-->
</appender>
But this did not work for me because im using the Azure Storage Emulator and the Append Blob operations are not supported by the emulator currently.
Still no idea how to go from XML to text file.
use AzureAppendBlobAppender will log into one file per day, but still inside the file it's all XML entries.
If you want to use the layout option, clone the source code from that link and change the source code of AzureAppendBlobAppender and build your own appender
private void ProcessEvent(LoggingEvent loggingEvent)
{
CloudAppendBlob appendBlob = _cloudBlobContainer.GetAppendBlobReference(Filename(_directoryName));
string content;
try
{
content = _lineFeed + RenderLoggingEvent(loggingEvent);
}catch(InvalidOperationException e)
{
//if no layout set, using xml
content = _lineFeed + loggingEvent.GetXmlString(Layout);
}
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(content)))
{
appendBlob.AppendBlock(ms);
}
}
Then you will be able to use your layout config inside of appender
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger [%M %C] - %message%newline" />
</layout>
First of all, try to convert the XML file to a txt file using the following blog tutorial
Then you could use other features rather than log4net if you want to append to textfile. the blobAppend functionality to append to a log file (text file as you require):
Your first take is use: BlobAppend and user SAS URI a sample looks as this:
CloudAppendBlob appendBlob = new CloudAppendBlob(new Uri("https://{storage_account}.blob.core.windows.net/{your_container}/append-blob.log?st=2017-09-25T02%3A10%3A00Z&se=2017-09-27T02%3A10%3A00Z&sp=rwl&sv=2015-04-05&sr=b&sig=d0MENO44GjtBLf7L8U%2B%2F2nGwPAayjiVSSHaKJgEkmIs%3D"));
appendBlob.AppendFromFile("{filepath}\source.txt");
Second:
I'd recommend checking this specific tutorial that is external, providing and example of blobAppend: https://dzone.com/articles/adding-to-an-existing-azure-blob
These should you get to the results you wish.

How do I setup a FileAppender logger from log4net for a .NET Standard 2.0 class library?

How do I setup a File Appender logger from log4net for a class library using .NET Standard 2.0? I don't have an AssemblyInfo.cs. I think I have a fundamental misunderstanding that's leading to my confusion with this, so this may be a simple answer, but I just started a class library that I want to be accessible to as many projects as possible in our solutions and have internal logging from the get-go.
Any and all suggestions/help is much appreciated.
There is nothing special about the AssemblyInfo.cs file. The attribute to configure log4net can be put in any file in the assembly.
That being said I would recommend against using log4net directly and instead use a ILogger from the NuGet package Microsoft.Extensions.Logging.Abstractions. This allows the consumer of your library use any logging system they want instead of being tied to only using log4net.
Not sure if I understood what you need/want but this is an idea of how the config file should look like
<log4net>
<root>
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="test.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger [%M %C] - %message%newline" />
</layout>
</log4net>
If i'm not mistaken, you don't need to add anything to your assemblyinfo (if you dont have one) but you need to add something like this on the startup
log4net.Config.XmlConfigurator.Configure();
(in my case webapi so I add it to my startup.cs)
I hope this helps!

log4net multiple loggers - properties

log4net v. 2.0.8
I've got some problems in multiple logging with log4net. I'm developing an application that works with n devices and I would like to have a single log file for each device.
log4net.config
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%envFolderPath{LocalApplicationData}/Foo/Device%property{DeviceID}/log.txt"/>
<appendToFile value="true"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="BEGIN LOGGING AT %date *** %property *** %newline" type="log4net.Util.PatternString" />
<param name="Footer" value="END LOGGING AT %date *** %property *** %newline" type="log4net.Util.PatternString" />
<param name="ConversionPattern" value="%date [%thread] %-5level %-5class{1} %-5method(%line) %message %newline" />
</layout>
<filter type="log4net.Filter.PropertyFilter">
<key value="Version" />
<stringToMatch value="1" />
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
and this is the code
public DeviceClass(string deviceID)
{
InitializeComponent();
GlobalContext.Properties["DeviceID"] = deviceID;
logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
logger.Debug("Hello world");
The problem is that if I have for example two devices I got just the first log file with the messages from all devices.
I'm not a Log4Net expert, but I believe that the log manager is going to initialize the logger and all of its appenders the first time that you call GetLogger for the given type/name (type under covers is translated to a string name). That being the case, it wouldn't re-initialize one per device. My suggestion for you is to try creating a composite of the type name and device ID using something like:
logger = LogManager.GetLogger($"{System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName}{deviceId}");

log4net doesn't produce a log file when my project is deployed with windows installer

When I log in my dev environment there is no problem but when I deploy with a windows installer no log files are produced:
<log4net>
<root>
<level value="ALL"/>
<appender-ref ref="LogFileAppender"/>
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<param name="File" value="C:\Logs\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>
I read that it may be that it cannot find the app.config but how do I tell it where to find it?
The issue was that log4net couldn't find the config file at run time after the dll was installed. Fixed by a suggestion here. In the end, my solution looked like this:
FileInfo fi = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "Assembly.dll.config");
log4net.Config.XmlConfigurator.Configure(fi);
_log.Info("Hello logging...");
Thank you for all of your input!
The app.config project file is turned to the the application.exe.config file after compilation. Maybe you are not deploying it with the installer?

Categories