Log4Net offline DB Support - c#

I am developing an asp.net web application and want to log my exceptions into SQL DB. For this I am using Log4Net AdoNetAppender to log info into SQL DB, now the problem occurs when the DB goes offline. Log4Net doesn't persist the log messages. So all messages get lost while the DB was offline, is there any way to retain the messages until the DB comes online and then log all the messages to DB once it become available. Although by using reconnectonerror value="True" it starts logging again when DB is available but all intermediate messages while DB was offline are not logged.
Or there exist any other approach to log exceptions in Db with offline support.

There is nothing with helps you out of the box with this. You can always log to a file (keep the last week or so) and the database. If for some reason there is a gab in the logging you can fallback on the file.
If you want the behavior you describe you can always implement your own appender. Let is inherrit from the adonetappender and add your failover code. However keep in mind that you do not want to create an appender which eat all your memory...

Related

Logging web services operations behind load balancer

I developed some web services will be installed on 4 different servers behind a load balancer mantains sessions.
I'm using c# and log4net.
The appenders are a RollingFileAppender and an AdoNetAppender.
I read from https://logging.apache.org/log4net/release/faq.html (section How do I get multiple process to log to the same file?)
If you use RollingFileAppender things become even worse as several
process may try to start rolling the log file concurrently.
RollingFileAppender completely ignores the locking model when rolling
files, rolling files is simply not compatible with this scenario.
I can't use RollingFileAppender with MinimalLock. But I want to log from the different servers to the same file.
I prefer to keep log4net but I'm interested also in other solution (not the linux syslog one). No commercial solutions allowed for costs.
Unfortunately, you'll discover that logging directly to the same file from multiple processes is not a very feasible option.
You have several alternatives:
Log to different files - each server can have a separate file
Send all your logs to one application, which will then log to the files. This will make your logging more brittle and require extra development effort.
Log to a database - databases are designed to have multiple processes writing to them at once
Log to a logging server - Seq, Stackify Retrace, and Azure Application Insights are some examples of solutions that are robust and designed to ingest logs from multiple applications - plus you get much better capabilities

Delete information from transaction logs at SQL Server

Console application uses sql request BACKUP DATABASE to create .bak file
https://technet.microsoft.com/en-us/library/ms191304(v=sql.105).aspx
It works good, but transaction logs contain information about this backup (and restore). How can I delete this information from transaction logs programmatically? Or can I backup/restore sql database without adding this information to transaction logs?
No you can't do any modification insiTransaction log
If you use SIMPLE recovery model, use CHECKPOINT to truncate the log.
If you are in FULL or BULK LOGGED, backup your log (you may need to repeat this operation if log is not truncated for some reason)
But what do you think it contains? There is no real data (reguarding backup) in there, only information about differential bitmap changes

C# windows service crash

I have a windows service which process text files using a file watcher, which is working fine (thanks to your previous help).
My other part of the project is a portal, where I access the processed data, see a log of all activities (which I'm using listview with an update panel and timer).
Now I found that whenever I access the messages or the log, my service crashes, as the page try to load the messages (over 70000 records for 3 days only).
It might be due to the big number of records my portal is trying to read while the service is writing a new records at the same time.
I'm using Entity Framework and my catch is throwing this error "Error: An error occurred while updating the entries. See the inner exception for details"
My service and portal are updating the same log table on the DB (but I don't think this is the reason).
any idea?

How to handle application configuration and data?

Microsofts preferred way to handle application configuration and runtime data seems obvious at a first glance: App.config, which will be stored in the application execution directory (C:\Program Files\ProductLocation in most cases.) where only privileged users have write access. (Makes sense to me, because a casual user shouldn't be able to alter essential application configurations).
For normal user configuration, there's a user.config which will be copied into each users personal application data directory (%APPDATA%).
But this leads to a few questions:
How can I alter configurations for every user without executing the process as administrator?
Where should I store application data that doesn't get deployed with the application, instead should be generated when the application is started the first time?
How is it possible to have e.g. dynamic connections strings, like for a database health monitor application?
I checked out the program data folder (%PROGRAMDATA% -> C:\ProgramData), but it seems this place is read-only for the standard user. (Windows Installer does create folders in here if needed, but they're all read-only.) -> What happend to %ALLUSERS%?
Example where the Microsoft way may fail in my eyes:
A financial application where every user should store his information in the same database (a SqlCE file db), where as the application has to run with user privileges (I don't want to be administrator to manage my wallet). The application needs connection to a database that isn't available at runtime and may be generated in during the first run using EntityFramework. So it could be possible that even the connection string has to be dynamic, and not configured in the app.settings where such information is fixed.
This is stupid! Users could read sensitive information from other users by directly accessing the file database!
-> Security is not only a file permission thing, there could also be database users, certificates, cryptography etc.)
Do I have to develop my own settings handler as a workaround to the Microsoft intended way?
I guess this question is asked a numerous times on SO, but every answer I found showed up workarounds, different solutions. Questions regarding "best practice" are closed immediately, so I tried to provide a practical example here.

Best way to read error messages from a Log4Net log file

I would like to display the log created by Log4Net on a web page in my admin interface.
Isnt there any methods available in the log4net library to read the error messages from the configured source (textfile or database)? At the moment I am using a database table to log all errors.
If not, there must be some third party libraries available that does this for me?
If you want an elegant solution to collect and search logs using log4net based logging, a syslog daemon like kiwi syslog in combination with log4net local or remote syslog appender would probably be the easiest way to do it. Logging in a database works, but in my opinion logs are of no concern to the application itself and should be kept away from it. For instance a failing database connection would probably not show up if the logs resided solely on the same database.

Categories