[CanBeNull] object Test() => null;
void UseTest() {
var p = Test();
var s = p.ToString(); // Warning: Possible 'System.NullReferenceException'
}
[NotNull] object Foo() {
return null; // Warning: Possible 'null' assignment
}
[StringFormatMethod("message")]
void ShowError(string message, params object[] args) { /* do something */ }
void Foo() {
ShowError("Failed: {0}"); // Warning: Non-existing argument in format string
}
void LogInfo([StructuredMessageTemplate]string message, params object[] args) { /* do something */ }
void Foo() {
LogInfo("User created: {username}"); // Warning: Non-existing argument in format string
}
Function Definition Table syntax:
[ContractAnnotation("=> halt")]
public void TerminationMethod()
[ContractAnnotation("null <= param:null")] // reverse condition syntax
public string GetName(string surname)
[ContractAnnotation("s:null => true")]
public bool IsNullOrEmpty(string s) // string.IsNullOrEmpty()
// A method that returns null if the parameter is null,
// and not null if the parameter is not null
[ContractAnnotation("null => null; notnull => notnull")]
public object Transform(object data)
[ContractAnnotation("=> true, result: notnull; => false, result: null")]
public bool TryParse(string s, out Person result)
[UsedImplicitly]
public class TypeConverter {}
public class SummaryData
{
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedWithFixedConstructorSignature)]
public SummaryData() {}
}
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors | ImplicitUseTargetFlags.Default)]
public interface IService {}
<configSections>
<section name="NLog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
${appsetting:item=mysetting:default=mydefault} - produces "mydefault" if no appsetting
See NLog Wiki
[Target("MyFirst")]
public sealed class MyFirstTarget : AsyncTaskTarget
{
public MyFirstTarget()
{
this.Host = "localhost";
}
public Layout Host { get; set; }
protected override Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken token)
{
string logMessage = this.RenderLogEvent(this.Layout, logEvent);
string hostName = this.RenderLogEvent(this.Host, logEvent);
return SendTheMessageToRemoteHost(hostName, logMessage);
}
private async Task SendTheMessageToRemoteHost(string hostName, string message)
{
// To be implemented
}
}
protected override Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken token)
{
return CustomWriteAsync(logEvent, token);
}
private async Task CustomWriteAsync(LogEventInfo logEvent, CancellationToken token)
{
await MyLogMethodAsync(logEvent, token).ConfigureAwait(false);
}
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
| Condition | Foreground Color | Background Color |
|---|---|---|
| level == LogLevel.Fatal | Red | NoChange |
| level == LogLevel.Error | Red | NoChange |
| level == LogLevel.Warn | Yellow | NoChange |
| level == LogLevel.Info | White | NoChange |
| level == LogLevel.Debug | Gray | NoChange |
| level == LogLevel.Trace | DarkGray | NoChange |
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
${basedir}/${level}.log
All ${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
[Target("MyFirst")]
public sealed class MyFirstTarget : TargetWithContext
{
public MyFirstTarget()
{
this.Host = "localhost";
}
public Layout Host { get; set; }
protected override void Write(LogEventInfo logEvent)
{
string logMessage = this.RenderLogEvent(this.Layout, logEvent);
string hostName = this.RenderLogEvent(this.Host, logEvent);
return SendTheMessageToRemoteHost(hostName, logMessage);
}
private void SendTheMessageToRemoteHost(string hostName, string message)
{
// To be implemented
}
}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
Asynchronous target wrapper allows the logger code to execute more quickly, by queuing messages and processing them in a separate thread. You should wrap targets that spend a non-trivial amount of time in their Write() method with asynchronous target to speed up logging.
Because asynchronous logging is quite a common scenario, NLog supports a shorthand notation for wrapping all targets with AsyncWrapper. Just add async="true" to the <targets/> element in the configuration file.
... your targets go here ...
]]>
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
This example causes the messages to be written to server1, and if it fails, messages go to server2.
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
This example causes the messages not contains the string '1' to be ignored.
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
This example works like this. If there are no Warn,Error or Fatal messages in the buffer only Info messages are written to the file, but if there are any warnings or errors, the output includes detailed trace (levels >= Debug).
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
This example causes the messages to be written to either file1.txt or file2.txt chosen randomly on a per-message basis.
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
This example causes each log message to be repeated 3 times.
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
This example causes each write attempt to be repeated 3 times, sleeping 1 second between attempts if first one fails.
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
This example causes the messages to be written to either file1.txt or file2.txt. Each odd message is written to file2.txt, each even message goes to file1.txt.
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this:
This example causes the messages to be written to both file1.txt or file2.txt
To set up the target in the configuration file, use the following syntax:
To set up the log target programmatically use code like this: