Dynamics 365 报表使用日志记录查询及相关扩展
获取用户查询报表的历史记录
select SUBSTRING( cl.Description,1,LEN(cl.Description)-1) reportname, --报表名称 除去最后的冒号
sur.FullName, --用户名
sur.DomainName, --域账号
ex.TimeStart --开始时间
from ReportServer.dbo.ExecutionLogStorage ex
inner join ReportServer.dbo.Catalog cl on cl.ItemID = ex.ReportID
inner join SystemUser sur on sur.SystemUserId = SUBSTRING(ex.Parameters,8,36)
where SUBSTRING( ex.Parameters,1,7) = 'userid=' -- 筛选用户账号登录的信息
注意:
- 如果报表重新部署了,则报表ID会发生变化导致历史记录查不到。
- ReportServer.dbo.ExecutionLogStorage 这张表好像只存储不到两个月的记录
但是这张表是系统自带的表 不敢乱搞 因为如果想要记录更久的记录只能曲线救国
备份表“曲线救国”
每天对 ExecutionLogStorage 表中新增的数据写到备份表中,查询数据时在备份表中查询数据。
在查询数据时遇到一个问题
-- 如下SQL 为在备份表中插入今天(2021年1月12日)的记录
insert into ReportServer.dbo.new_reports_log select * from ReportServer.dbo.ExecutionLogStorage where TimeStart > '2021-01-12 00:00:00.00'
执行时报错了
仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表’ReportServer.dbo.new_reports_log’中的标识列指定显式值。
这是因为:
在有自增长的SQL表格里面插入指定ID的数据的时候,会禁止操作
所以需要两点:
- 把 ReportServer.dbo.new_reports_log 的 IDENTITY_INSERT 设置为 ON
- 不能省略列名
因此上面的SQL要改成如下:
set identity_insert ReportServer.dbo.new_reports_log ON;
INSERT ReportServer.dbo.new_reports_log
(LogEntryId,InstanceName,ReportID,UserName,ExecutionId,RequestType,Format,Parameters,
ReportAction,TimeStart,TimeEnd,TimeDataRetrieval,TimeProcessing,TimeRendering,Source,Status,ByteCount,[RowCount],AdditionalInfo)
SELECT LogEntryId,InstanceName,ReportID,UserName,ExecutionId,RequestType,Format,Parameters,
ReportAction,TimeStart,TimeEnd,TimeDataRetrieval,TimeProcessing,TimeRendering,Source,Status,ByteCount,[RowCount],AdditionalInfo
FROM ReportServer.dbo.ExecutionLogStorage
WHERE TimeStart > '2021-01-12 00:00:00.00';
SET identity_insert ReportServer.dbo.new_reports_log OFF;