Link to home
Start Free TrialLog in
Avatar of Sri
Sri

asked on

C# Logging

What is the best practice to log REST API response messages, these responses are too huge.
I am working on a requirement to log the responses to files
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Hi Tesr,

When it comes to logging to files.... I think no one beats https://logging.apache.org/log4net/

It is easy to configure and use. Also it gives blazing fast performance compared to other loggers.

Use File appender in the configuration and you are good to go.

<appender name="FileAppender" type="log4net.Appender.FileAppender">
2.    <file value="log-file.txt" />
3.    <appendToFile value="true" />
4.    <layout type="log4net.Layout.PatternLayout">
5.        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
6.    </layout>
7.</appender>

Open in new window


Regards,
Chinmay.
HI Tesr KTes,

Here you can find best practices for logging in .NET (log4net and NLog)

Ultimate log4net:

https://stackify.com/log4net-guide-dotnet-logging/

Ultimate NLog :

NLog is a very popular logging framework for .NET. It is second only to log4net in popularity but is much newer and has a few unique features.

https://stackify.com/nlog-guide-dotnet-logging/
Avatar of Sri
Sri

ASKER

Thank you Chinmay and Tapan, mine is a custom logging tool. Sorry i would have told this earlier.
But i like the idea of appending file need to check how this is feasible in our custom logging tool
I have idea about these logging tools but i am looking for best practices to log huge data.

Thanks once again for your quick response.
Hi Sri,

This being a REST API I see there are couple of points to keep in mind (these are based on log4Net and EntLib)
1. You will definitely use singleton pattern so that throughout the life cycle of your application only once the logger in instantiated
2. You will need a Read/Write permission on the file system - which can be achieved by impersonation but has a performance trade off. Other way is to give your service account(in whose context the service will run) read/write permission which can have security implications.
3. You would want to use async calls when you are actually performing the write operation to the log.
4. You will want to create a mechanism so that your logger keeps rotating the files i.e. on a regular interval old files are archived/deleted

i have couple of suggestions - I know you want to use custom logging but can you share the requirements around the use of custom logging tool?

Also if your APIs are hosted on IIS(or for that any web server) why not use that web server's logging feature - which are tried and tested for high volume usage scenarios ( It is the same argument I have for not using a custom logging tool though :), why reinvent the wheel?).

Regards,
Chinmay.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.