As a solution for development environment, a new table [NLogLastCleanup] was added to store last time of table cleanup. And the command which enters new records is responsible for old records cleanup now. Also, we pass "cleanup" and "removal" periods via NLog parameters.
<!--Database-->
<target name="database" xsi:type="Database" connectionStringName="nlog">
<commandText>
<![CDATA[
--insert log message
insert into [dbo].[NLog] ([Date], [Level], [Logger], [User], [Message], [Exception], [MachineName], [Pid])
values (@date, @level, @logger, @user, @message, @exception, @machinename, @pid);
--
-- Cleanup old records
-- Cleanup should run once per day
-- Remove records, which are older than a week
--
declare @lastCleanup datetime
select top 1 @lastCleanup = [LastCleanupDate] from [dbo].[NLogLastCleanup]
if @lastCleanup < DATEADD(Day, -CAST(@cleanupPeriod as int), GETDATE())
begin
update [dbo].[NLogLastCleanup] set [LastCleanupDate] = getdate()
delete [dbo].[NLog] where [Date] < DATEADD(Day, -CAST(@removePeriod as int), GETDATE())
end
]]>
</commandText>
<parameter name="@cleanupPeriod" layout="1" size="4" scale="0"/>
<parameter name="@removePeriod" layout="7" size="4" scale="0"/>
<parameter name="@date" layout="${date:format=yyyy\-MM\-dd HH\:mm\:ss.fff}"/>
<parameter name="@machinename" layout="${machinename}" />
<parameter name="@pid" layout="${processid}" />
<parameter name="@level" layout="${level}"/>
<parameter name="@logger" layout="${logger}"/>
<parameter name="@user" layout="${identity:authType=false:isAuthenticated=false}" />
<parameter name="@message" layout="${message}"/>
<parameter name="@exception" layout="${plainExceptionFormat}"/>
</target>
No comments:
Post a Comment