Hi guys,
I'm trying to implement custom agent that could make rotation of CS logs.
I've run into one problem, that I cannot overcome in any way.
Here is how I implemented rotation process itself.
- copy existing log file and rename the copy by appending date of rotate operation. (CS is keeping file reference to the original log file, so I cannot remove original file and create a new one. If I do so, new log file is created, but nothing is written in there after that. It remains empty all the time.)
- open original log file in WriteMode and close it. (In this way I can clear content of the original log file without deleting it and creating from scratch)
Here is how my function looks like:
function void Rotate(String log, Date now)
ListlogList
Stringext, newName
Dynamic f
logList = Str.Elements(log, ".")
ext = logList[-1]
newName = log + Date.DateToString(now, "_%d%m%Y%H%M") + "." + ext
if ( !$KERNEL.FileUtils.Copy(log, newName))
.WriteLog([MYAGENT_ERRMSG.CouldNotGetWriteAccessToFile] + log)
else
f = File.Open(log, File.WriteMode)
if IsError (f)
.WriteLog([MYAGENT_ERRMSG.CouldNotGetWriteAccessToFile] + " : " + Error.ErrorToString(f))
else
File.Close(f)
end
end
end
But the problem with all this stuff, is that after I clear content of the original file, CS doesn't write logs from the beginning of this file, but from the last point where it finished last write operation. In other words, size of the original files doesn't decrease after log rotation. Content which was copied to the rotated log is replaced by some "NULL" characters, but still exists in the original log file.
So I'm wondering about:
1) Whether my approach is correct or not?
2) Can I somehow tell CS to move file write pointer to the beginning of the file ?
3) Are there any other approaches or tools to implement this functionality?