3

I have a tool that my team uses for various administration tasks, it handles various remote calls, Active Directory look ups, calls scripts and more to support thousands of machines. I have about a dozen people actively using it, but for geekdom and further development I would like to build in some statistic and usage logging (nothing personal, just to see what is used and how much).

My question, is what would be a good (or even best) way in .NET (C# specifically) to log this information?

My first thought was a TXT file or even a XML file, but I don't know that I really want to load a file of unknown size later down the road over and over. So I thought about SQLite, but I do not want to install .SQlite on each machine, although perhaps I can embed it in the exe (Its run from a share, not an install for most users although I also have a setup package built for it.

I really don't want to use lots of text files, just for the sake of clean folders.

Any suggestions that would meet the following criteria:

  1. Light weight
  2. Fast
  3. No installs on the client
  4. Stable Hard to corrupt
  5. Local Database like

I tried to google this, but I wasn't getting anything that really made sense to me, it was probably that I didn't know what I should Google, as this is not a typical usage model for me.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Austin T French
  • 5,022
  • 1
  • 22
  • 40

3 Answers3

2

Microsoft SQL Server Compact is an embeddable, free, redistributable database engine with good APIs for .NET. No install is required; you just make a reference to the database engine library from your .NET project and that's it. Databases are simple files that you can store anywhere and move around like any other.

It is quite similar to the "real" SQL Server, so data migration between the two should be easy in case you need it, and there are plenty of tools to help you with this.

CesarGon
  • 15,099
  • 6
  • 57
  • 85
  • I am looking at the page now, and I see perhaps two possible problems: 1. Does it require anything new installed on the clients? 2. It says it is embedded into the application / folder? So if its run from a share who's data takes over? Oddly enough I want per machine / user information that can be recovered easily. – Austin T French Mar 15 '13 at 17:07
  • I see we do have 3.5 already installed so this might work, pending how the DB actually resides. – Austin T French Mar 15 '13 at 17:10
  • 1. The database engine is implemented as a DLL. You will need to redistribute that DLL as part of your application binaries. You can put it in a share if you like. No installation as such, though. – CesarGon Mar 15 '13 at 17:10
  • 2. That's up to you. There is an "open database" method which you call, passing in a filename. You can open a common file to all users, or a per-user file. You decide where to store your files. – CesarGon Mar 15 '13 at 17:10
  • It will be in the %userProfile%\My Documents\ directory. Last question on this one, does the compact framework support on the fly file generation if no previous document exists? I have honestly only tried this with .txt, html. – Austin T French Mar 15 '13 at 17:16
  • Are you talking about the .NET Compact Framework, or SQL Server Compact? They are totally different things. – CesarGon Mar 15 '13 at 17:18
  • You can create a new, empty database file with SQL Server Compact by calling `SqlCeEngine.CreateDatabase()`. Is this what you're after? – CesarGon Mar 15 '13 at 17:20
  • I am looking at the SQl Server Compact. – Austin T French Mar 15 '13 at 17:24
  • OK; in that case, as I said, yes: you can use `SqlCeEngine.CreateDatabase()` to create a new, empty database file. More info here: http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceengine.createdatabase(v=vs.100).aspx – CesarGon Mar 15 '13 at 17:25
2

You could use db4o. It is an object-oriented database (no SQL) which saves your objects directly - no need for object mapping, attributes or any configuration overhead, just take your POCOs and save them. You can use different approaches for queries, such as Query by example or SODA (which is similar to criteria for Hibernate), it also has full LINQ-support. You can use db4o as embedded database (uses a file), the memory footprint is very small. No installation required, just include the lib into your project.

Example:

var persons = db.Query<Person>( s => s.Age > 50 );
cara
  • 1,012
  • 1
  • 9
  • 15
0

Use Log4Net and decouple that data sink(s) from your apps. Log4Net lets you log to files, databases, the Windows event log, and others. You can even direct log output to different destinations based on the assembly/class from which it comes, by logging level (Fatal, Error, Warning, Info, etc.) and other filter criteria.

You can even write your own appenders to do just what you need.

All through changing your log4net configuration. You can even adjust the log4net configuration while your app is running. For instance, consider a long-running daemon with logging set to catch fairly high level messages across the app. If you log suddenly starts showing a sudden increase in errors logged, you might want to dial the logging down to a more granular level to get an idea of what's going on. Or perhaps dial it down to a very granular level on the suspect class or assembly to isolate the problem details.

With log4net, you can do that just by tweaking the config file and without bouncing the daemon.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135