Logging basics - michaelrandrup/TinyLog GitHub Wiki
The Log class is the foundation for all logging with TinyLog. This is where you write and read log entries. In most cases having just one log per application will be sufficient, but you can have as many logs as you like.
The static method TinyLog.Log.Default
will return an empty default log object to use.
To create log objects, you use the static TinyLog.Log.Create()
method. As a minimum, you need to specify one LogWriter
when using the Create() method.
The log encapsulates the following support objects:
- One or more
LogWriter
objects -- The log writers are responsible for writing your log entries to a backend storage of some kind - Zero or more
LogFormatter
objects -- The log formatters are responsible for parsing and formatting any custom data, that you would like to store with your log entries - Zero or more
LogSubscriber
objects -- The log subscriber receives events about incoming log entries. You can configure log subscribers with a filter, so they only recieve certain types of log entries. The log subscriber can then do further processing, such as emailing the log entries to a group of people, etc. - Zero or more
LogReader
objects -- The log readers are responsible for reading log entries from the backend storages. You can use log readers to administer and view your logs
When you create a new log or use the default log, you need to register the above objects to get your log operational. To do this, you use the various Register methods on the Log object. For example, if you want to use an SQL Server as the backend storage, and you want to serialize your custom log entry data using json, you can create a log as follows:
// Configure the default log for SQL Server storage and json formatting
TinyLog.Log.Default.RegisterLogWriter(new SqlLogWriter("MyConnectionString", "MyLogTable"));
TinyLog.Log.Default.RegisterLogFormatter(new JsonSerializationFormatter());
// Create a new log with SQL Server storage and Xml formatting
TinyLog.Log MyLog = TinyLog.Log.Create(
logWriters: new LogWriter[] { new SqlLogWriter("MyConnectionString", "MyLogTable") },
logFormatters: new LogFormatter[] {new XmlSerializationFormatter()});
MyConnectionString is a SQL Server Connection string, and MyLogTable is the name of the table that stores the log entries.
You can add multiple log writers to your logs. By default, when you write a log entry, all LogWriter
objects will write that entry to the backend storage. All LogWriters supports filtering, so only certain events are sent to the particular writer.
Example: If you have two tables in your SQL Database that serves as log tables, you could write all Critical entries to one table and all other severities to another. To achieve this, setup your log as below:
// Configure two LogWriters. One for critical and error events and one for all other events
TinyLog.Log.Default.RegisterLogWriter(new SqlLogWriter("MyConnectionString", "MyCriticalLogTable")
{
Filter = LogEntryFilter.Create(severities: new LogEntrySeverity[] { LogEntrySeverity.Critical, LogEntrySeverity.Error})
});
TinyLog.Log.Default.RegisterLogWriter(new SqlLogWriter("MyConnectionString", "MyDefaultLogTable")
{
Filter = LogEntryFilter.Create(severities: new LogEntrySeverity[] { LogEntrySeverity.Information, LogEntrySeverity.Warning, LogEntrySeverity.Verbose })
});
In the example above the first log writer will only be used for Error and Critical log entries. The other LogWriter will be used for all other severities.
This is very simple, to create a log entry, simply create an instance of the LogEntry
object and call one of the WriteLogEntry()
methods on the Log. The LogEntry
object contains multiple static shorthand methods, to help you create instances.
In this example, we use the Create() method on the LogEntry
:
// Create a simple entry with the 'Information' severity
TinyLog.Log.Default.WriteLogEntry(LogEntry.Create("My Title", "My Message"));
// Create a more detailed log entry
TinyLog.Log.Default.WriteLogEntry(LogEntry.Create("My Title", "My message", "A source", "An area", LogEntrySeverity.Warning));
In this example, we register some custom data with a warning log entry:
// Create a warning log entry for a failed login with custom data
var UserFailedLogin = new
{
UserName = "[email protected]",
LoginTime = DateTime.Now
};
TinyLog.Log.Default.WriteLogEntry<object>(LogEntry.Warning("Login failed", "The password was expired", "Login", "/Home/Login"), UserFailedLogin);
Notice: In order to use the example above, you need to register a
LogFormatter
capable of serialized an anonymous object. For example theJsonLogFormatter