SHARE
TWEET

Untitled

a guest Feb 19th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE TABLE [dbo].[NLogEntries](
  2.   [Id] [bigint] IDENTITY(1,1) NOT NULL,
  3.   [Origin] [nvarchar](100) NOT NULL,
  4.   [LogLevel] [nvarchar](20) NOT NULL,
  5.   [Message] [nvarchar](3600) NOT NULL,
  6.   [CreatedOn] [datetime] NOT NULL,
  7.   [OrderId] [int] NULL --Custom field!
  8. )
  9.    
  10. <target type="Database" name="database" connectionstring="Server=localhost;Database=NLog;Trusted_Connection=True;">
  11.   <commandText>
  12.     INSERT INTO NLogEntries ([Origin], [Message], [LogLevel],[CreatedOn],[OrderId]) VALUES (@Origin,@Message,@LogLevel,@Date, @OrderId);
  13.   </commandText>
  14.   <parameter name="@Date" layout="${date}"/>
  15.   <parameter name="@Origin" layout="${callsite}"/>
  16.   <parameter name="@LogLevel" layout="${level}"/>
  17.   <parameter name="@message" layout="${message}"/>
  18.   <parameter name="@OrderId" layout="${orderId}"/> <!-- custom field! -->
  19. </target>
  20.    
  21. var logger = LogManager.GetCurrentClassLogger();
  22. var orderId = 123;
  23. logger.Debug("What is going on here", orderId);
  24.    
  25. <target type="Database" name="database" connectionstring="Server=localhost;Database=NLog;Trusted_Connection=True;">
  26.   <commandText>
  27.     INSERT INTO NLogEntries ([Origin], [Message], [LogLevel],[CreatedOn],[OrderId]) VALUES (@Origin,@Message,@LogLevel,@Date, @OrderId);
  28.   </commandText>
  29.   <parameter name="@Date" layout="${date}"/>
  30.   <parameter name="@Origin" layout="${callsite}"/>
  31.   <parameter name="@LogLevel" layout="${level}"/>
  32.   <parameter name="@message" layout="${message}"/>
  33.   <parameter name="@OrderId" layout="${gdc:OrderId}"/> <!-- custom field! -->
  34. </target>
  35.    
  36. var logger = LogManager.GetCurrentClassLogger();
  37. GlobalDiagnosticContext["OrderId"] = 123;
  38. logger.Debug("What is going on here"); //If you use the logging configuration above, 123 will be logged to the OrderId column in your database
  39.    
  40. var logger = LogManager.GetCurrentClassLogger();
  41.  
  42. MDC.Set("OrderId", 123);
  43. MDC.Set("user", HttpContext.Current.User.Identity.Name);
  44. // ... and so on
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top